List of Posts in a Custom Field

I have a question. I have a custom post type called Place. I have a situation that there can be place in place. I want to have a custom field parent_place where I could choose a parent of place.
The Place can have none or one parent.

Is there any posibility to list all posts (type = place) in custom field and choose one of them? In plugins for custom fields I can choose dropbox, radio, etc, but I have to type values. How can I automatically fill in a list of all posts which type is Place?

Related posts

Leave a Reply

2 comments

  1. As Michal Mau points out, ACF is easy and effective to automate this kind of Custom Field/Meta Box management.

    The manual mode uses the action hooks add_meta_box and save_post. In this example, you’d have to adjust the indicated arrays to simply array( 'place' ). Check comments for details:

    add_action( 'add_meta_boxes', 'add_custom_box_wpse_94701' );
    add_action( 'save_post', 'save_postdata_wpse_94701', 10, 2 );
    
    function add_custom_box_wpse_94701() 
    {
        // Post types to insert the meta box. Adjust array <-------
        foreach( array( 'post', 'portfolio' ) as $pt )
            add_meta_box(
                'sectionid_wpse_94701',
                __( 'Custom parent' ), 
                'blogroll_box_wpse_94701',
                $pt,
                'side'
            );
    }
    
    
    function blogroll_box_wpse_94701() 
    {
        global $post, $typenow;
    
        // Get all posts of a type, excluding the current post
        $args = array(
            'numberposts' => -1,
            'post_type'   => $typenow,
            'post_status' => 'publish,future',
            'exclude'     => $post->ID,
        );
        $get_posts = get_posts( $args );
    
        $saved = get_post_meta( $post->ID, 'custom_parent', true);
    
        // Security
        wp_nonce_field( plugin_basename( __FILE__ ), 'noncename_wpse_94701' );
    
        // Dropdown
        echo '<select name="custom_parent" id="custom_parent">
            <option value="">- Select -</option>';
        foreach ( $get_posts as $parent_post ) 
        {
            printf(
                '<option value="%d" %s> %s</option>',
                $parent_post->ID,
                selected( $saved, $parent_post->ID, false),
                $parent_post->post_title
            );
        }
        echo '</select>';
    }
    
    
    function save_postdata_wpse_94701( $post_id, $post_object ) 
    {
        // Verify auto save 
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
            return;
    
        // Security
        if ( 
            !isset( $_POST['noncename_wpse_94701'] ) 
            || !wp_verify_nonce( $_POST['noncename_wpse_94701'], plugin_basename( __FILE__ ) ) 
            )
            return;
    
        // Allowed post types. Adjust array <-------
        $allowed_post_types = array( 'post', 'portfolio' );
        if ( !in_array( $post_object->post_type, $allowed_post_types ) )
            return;
    
        // Process post data
        if ( isset( $_POST['custom_parent'] )  )
            update_post_meta( $post_id, 'custom_parent', $_POST['custom_parent'] );
        else 
            delete_post_meta( $post_id, 'custom_parent' );
    }
    

    enter image description here
    enter image description here