Dropdown to set parent page

This topic contains 3 reply and 2 voices, and was last updated by Daniel 10 years, 9 months ago
Viewing 3 Posts - 1 through 3 (of 3 total)
Author Posts
February 10, 2014 at 6:34 pm 15277
Daniel 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?
February 10, 2014 at 8:58 pm 15279
Daniel Daniel

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.

February 10, 2014 at 10:51 pm 15282
Tareq Hasan Tareq Hasan

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

February 11, 2014 at 2:17 pm 15309
Daniel Daniel

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

Viewing 3 Posts - 1 through 3 (of 3 total)