I apologize in advance, I searched for an answer before posting this question but didn't see it, I saw some variations but couldn't quite figure it out.

I have 2 custom post types: Buildings and Apartments. I would like to assign a building to every apartment via a metabox dropdown.

This is what I have in my function.php file:

function get_myposttype_options( $query_args ) {

$args = wp_parse_args( $query_args, array(
    'post_type' => 'buildings',
) );

$posts = get_posts( $args );

$post_options = array();
if ( $posts ) {
    foreach ( $posts as $post ) {
        $post_options[ $post->ID ] = $post->post_title;

return $post_options;

This is my Metbox

 $meta_boxes[] = array(
'id' => 'buildingdetails',
'title' => 'Add to Building',
'pages' => array( 'property' ),
'context' => 'normal',
'priority' => 'high',

// List of meta fields
'fields' => array(

'name' => '',
'id' => $prefix . 'test_select',
'type' => 'select',
'options' => get_myposttype_options(),


Apartment with building metabox

Any help would be immensely appreciated.

  • 1
    Why are you doing an SQL query? I strongly recommend you use WP_Query instead $query = new WP_Query( array( 'post_type' => 'buildings'));
    – Tom J Nowell
    Mar 8, 2014 at 5:41
  • 1
    Also can you provide more context for your metabox? The array you posted is not the standard WordPress metabox API so a little context and explanation, perhaps a screenshot of what the metabox that gives you looks like
    – Tom J Nowell
    Mar 8, 2014 at 5:43
  • The database query portion of the code is from the now discontinued theme open house. The current theme is realto which is on Themeforest, of which I'm using the local metabox info. The apartments (Property) CPT is hardcoded into the theme, I used ECPT to create the buildings post type.
    – Marco
    Mar 8, 2014 at 22:09
  • The metabox API you're using is not standard WordPress, and unless we can see the code it relies on in full, an answer to your question is impossible. It's quite an ask to send a potential answerer to themeforest to purchase the theme you're using. You should rephrase your question to talk about the standard Metabox API, else your question is too localised and offtopic and will be closed
    – Tom J Nowell
    Mar 8, 2014 at 22:15
  • let us continue this discussion in chat
    – Tom J Nowell
    Mar 8, 2014 at 22:16

1 Answer 1


You'll want to create a standard metabox via the official APIs, how to do that is beyond this answer, what I'll talk about is your dropdown.

What you need to save is the Post ID of the building, so something like this pseudocode:

<select name="....">
$query = new WP_Query( array( 'post_type' => 'buildings' ));
if ( $query->have_posts() ) {
    while( $query->have_posts() ) {
        <option value="<?php the_ID(); ?>" <?php selected( get_the_ID(), $current_building ); ?>><?php the_title(); ?></option>

Now that you have the input sorted, treat it as any other input in a metabox. The saving and creation of the box is the same as normal

  • Hey Tom, I tried the above but couldn't get it to work. I followed some of the resources you provided and came up with an updated question, code, and almost answer.
    – Marco
    Mar 9, 2014 at 14:24
  • The links I gave you in chat wont help with the metabox library your theme uses, only the theme author can help you there, use a standard metabox instead. Attempting to use your themes metabox API as the basis of your question will get you no help ( as its not possible to help you ).
    – Tom J Nowell
    Mar 9, 2014 at 14:26
  • Maybe you can provide an example of how it should be done? The metabox I'm using above is passing the function, and it works pulling in the buildings post type but the word "Array" appears in the dropdown instead of the title.
    – Marco
    Mar 9, 2014 at 15:00
  • Got it working. I updated my question with the how it works.
    – Marco
    Mar 9, 2014 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.