0
votes

My theme contains two custom post types, Biographies and Places. In the Places post type I have a meta box that contains an HTML select element. In this select element I want to have a list of all the Biographies post type titles.

I thought I would be able to do a WP_Query in the add_meta_box() $callback argument function to populate the list, but this does not seem to work. How would I go about doing this?

Thanks.

1
have you tried anything yet? - Mark
Well obviously I have tried using WP_Query without any luck, but since I have modified what is listed here and was able to pull the information that I needed. But I still feel that this is not the proper way of going about it. I'd still like to know if there is a better way. - user3427302

1 Answers

0
votes

this is based on wptuts tutorial

use this as a case

case 'post_list':  
$post_types = $field['post_type'];
        $wp_cats = array();
    echo '<select name="'.$field['id'].'" id="'.$field['id'].'"> 
            <option value="">Chosse A Page</option>'; // Select One  
    foreach ( $post_types as $post_type ) {

        $pages = get_pages( array( 'post_type'=>$post_type, 'posts_per_page'=>-1, 'post_status'=>'publish', 'depth'=> 0 ) );

        $obj = get_post_type_object( $post_type );
        // output custom post type archive with link
        foreach ( $pages as $page ) {
       $wp_cats[$page->ID] = $page->post_title;
       // output posts with link
       if ( $post_type !== 'page' ){
           $title = $page->post_name;

           $url = home_url().'/'.$post_type.'/#'. str_replace('-', '', $title);
         echo '<option value="'.$url.'" ';
       if ($meta == $url) { echo 'selected="selected"'; }
       echo '>&nbsp;-&nbsp;'. $page->post_title .'</option>';  
       }else{
       echo '<option value="' . get_permalink($page->ID).'" ';
       if ($meta == get_permalink($page->ID)) { echo 'selected="selected"'; }
       echo '>&nbsp;-&nbsp;'. $page->post_title .'</option>';}

         }
       }              
    echo '</select><br /><span class="description">'.$field['desc'].'</span><br /><br /><strong style="color:#00bbe2; text-transform:uppercase;">Or</strong>';  
break;