1

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.

  • 1
    Have you checked the database to see if the information is updated? I suspect that it is (assuming the code above is not broken). The problem is that some or all of the other Core functions that deal with posts types won't know or care, as this is not supposed to be a hierarchical type. If you do this, you will have to write a lot of code to manipulate the CPT. – s_ha_dum Aug 29 '13 at 15:01
  • This is a not-issue. wp_update_post skip to update parent if the post type is not hierachical. If you want to force parent, use direct $wpdb query. Also note that the slow down if hierachical posts are a lot, involves only admin pages, not frontend. Finally, note that as @s_ha_dum said, you probably will have issue (inaxpected behaviors) with some core functions and (most probably) with rewrite rules, too. – gmazzap Aug 29 '13 at 16:38
  • @ s_ha_dum : I checked the database to see if the info is updated, and actually it's updating only for page and post but not for custom post type. – Onto neuu Aug 29 '13 at 22:11
  • @G.M, you right maybie I should try to have my cpt hierachical. – Onto neuu Aug 29 '13 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.