I’m trying to update post_parent of wp_posts in WordPress database.
I’ve created a custom post type with capability_type = post , but I want it to behave like a page: hierarchical, allowing parent to be specified.
Since I’ll have a lot of entries, I couldn’t set my custom post type as hierarchical. So I decided to update manually my custom post type post_parent entry.
From the codex :
Note for hierarchical: this parameter was planned for Pages. Be careful, when choosing
it for your custom post type – if you are planning to have many
entries (say – over 100), you will run into memory issue. With this
parameter set to true WordPress will fetch all entries of that
particular post type, together with all meta data, on each
administration page load for your post type.
Here is my code:
I tried $wpdb->update
, but no success
if( get_field('_page_parent') ) { $infos_page = get_field('_page_parent'); $parent_id = $infos_page[0]; // $wpdb->update( $table, $data, $where, $format = null, $where_format = null ); //$wpdb->prepare("UPDATE $wpdb->posts SET post_parent = %d WHERE ID = %d", $parent_id, $child_id); $my_post_infos = array( 'ID' => $child_id, 'post_parent' => $parent_id ); // unhook this function so it doesn't loop infinitely remove_action('save_post', 'set_dossier_parent'); // update the post, which calls save_post again wp_update_post( $my_post_infos ); // re-hook this function add_action('save_post', 'set_dossier_parent'); }
Any help would be greatly appreciated.