How do you modify the ‘post_parent’ of a custom post type?

I want to be able to edit the post_parent of a custom post type. Basically in my use case I want to mimic how WordPress uses attachments: have a custom post type that is sort of a subtype to post or any other post type. WordPress uses the post_parent field on the wp_posts table to link attachments to their parent posts so I want to be able to do the same. I’ve tried to use wp_update_posts but it seems to time out the connection when I try to call it during a post save. Is there a way of editing the post_parent directly?

Related posts

Leave a Reply

2 comments

  1. Hi @Manny Fleurmond:

    You can add the following HTML to a post metabox you’ll have an edit field that lets you edit the raw post_parent ID. Maybe with this knowledge you can build what you need?

    <input type="text" id="parent_id" name="parent_id" 
           value="<?php echo $post->post_parent; ?>" />
    
  2. If I well understood the question you can set support for page-attributes when you register a CPT like this:

    add_action( 'init', 'register_products_post_type' );
    function register_products_post_type() {
        $labels = array(
            'name' => 'Products'
        );
    
        $args = array(
            'labels'        => $labels,
            'public'        => true,
            'rewrite'       => array(
                'with_front'    => false
            ),
            'has_archive'   => true,
            'hierarchical'  => true,
            'menu_position' => 5,
            'supports'      => array( ..., 'page-attributes', ... )
        );
    
        register_post_type( 'products', $args );
    }
    

    A listbox will appear in the manage post page.