Home Forums Plugin Support WP User Frontend Pro Dropdown to set parent page

This topic is: resolved

This topic contains 3 replies, has 2 voices, and was last updated by  Daniel 5 years, 4 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #15277

    Daniel
    Member
    Post count: 8

    This plugin is really fantastic.
    I am currently using a custom hook to pull in some extra elements, but am having a bit of an issue with setting the parent page for the page I am editing/creating.
    I've checked in the backend and in the database and it should be set with an ID in the field post_parent.
    Fair enough, I have the following code

    <?php
    //Check if this is an existing post
    if (isset( $post_id )) {
    //Fetch the post_parent
    $parentquery = new WP_Query( ‘page_id='.$post_id );
    while ( $parentquery->have_posts() ) {
    $parentquery->the_post();
    echo ‘postID is: ‘ . $post_id . ‘<br>';
    echo ‘Is a page' . is_page() . ‘<br>';
    echo ‘The title is' . $post->post_title . ‘<br>';
    echo ‘Post Parent is: ‘ . $post->post_parent . ‘<br>';
    $hasparent = $post->post_parent;
    var_dump($hasparent);
    }
    wp_reset_postdata();
    //Reset the query
    //Set the arguments with a selected parent if possible, and exclude the current page from the list and limit it to top level pages.
    $parentargs = array(
    ‘depth' => 1,
    ‘selected' => $hasparent,
    ‘exclude' => $post_id,
    ‘name' => ‘post_parent');
    } else{
    $parentargs = array(
    ‘depth' => 1,
    ‘name' => ‘post_parent');
    }
    //output a dropdown with all pages
    wp_dropdown_pages($parentargs); ?>

    Everything seem hunky-dory, and it fetches and selects the correct option. When I post though, It resets the post_parent to 0.

    I post the data with a function with the following

    function update_my_pagefields( $post_id ) {
    if ( isset( $_POST[‘post_parent'] ) ) {
    update_post_meta( $post_id, ‘post_parent', $_POST[‘post_parent'] );
    }
    }

    add_action( ‘wpuf_add_post_after_insert', ‘update_my_pagefields' );
    add_action( ‘wpuf_edit_post_after_update', ‘update_my_pagefields' );

    Can anyone help me shed any light on this?

    #15279

    Daniel
    Member
    Post count: 8

    OK, now I feel silly. After a lot of researching and googling, I got a hint that the problem was the way I tried to save the data using update_post_meta()

    That function works only on custom meta-fields.

    However, it was a lot easier to update using a query through the$wpdb->query

    Example.

    if ( isset( $_POST[‘post_parent'] ) ) {
    global $wpdb;
    $parentint = (int) $_POST[‘post_parent'];
    $wpdb->query(
    $wpdb->prepare(

    UPDATE $wpdb->posts
    SET post_parent = %d
    WHERE ID = %d
    “,
    $parentint, $post_id
    )
    );
    }

    So yeah, this subject can be closed. Hope this helps some one else trying to do the same.

    #15282

    Tareq Hasan
    Keymaster
    Post count: 2831

    Or, you could use this:
    [php]wp_update_post( array(‘ID' => $post_id, ‘post_parent' => $post_parent ) );[/php]

    #15309

    Daniel
    Member
    Post count: 8

    Thank you Tareq, yeah, I guess that is always an option. ๐Ÿ™‚ And it is a lot more elegant. Will give it a whirl.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Dropdown to set parent page’ is closed to new replies.