WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I use the following code in a (wpalchemy) custom meta box, everything works fine. That is until I pressed 'add new'. At this point, the title, slug, featured image, etc.. are being pre-populated with the content from my 'products' post type.

The code builds a drop down within my 'case studies' post-type, and populates it with post titles/ids from my 'projects' post type.

How can I use the loop in the backend like this without messing stuff up. I tried with get_posts as well as WP_Query.

global $post;
$args = array( 'post_type' => 'projects', 'posts_per_page' => 200 );
$custom_posts = get_posts($args);

foreach($custom_posts as $post) : setup_postdata($post);
  echo '<option value="' . get_the_id() . '"';
  echo '>' . get_the_title() . '</option>' . "\n";

Edit: A little more background:

I'm using the wpalchemy class for custom meta boxes. The following code is included in my functions.php file, after /wpalchemy/MetaBox.php https://github.com/farinspace/wpalchemy/blob/dev/wp-content/wpalchemy/MetaBox.php

$assoc_project = new WPAlchemy_MetaBox(array(
    'id' => '_assoc_project',
    'title' => 'Associated Project',
    'template' => get_stylesheet_directory() . '/metaboxes/case-studies-assoc_proj-meta.php',
    'types'=> array('case-studies')

Edit 2: Update

I've tried and replicated this problem accross multiple browsers on my PC, but I just had a co-worker try on Ffox/mac and they don't have the problem. I don't understand why this would be platform specific - but there it is...

share|improve this question
what hook are you using to run that code? – mrwweb Jun 4 '12 at 21:01
@mrwweb not really sure, as I'm using the wpalchemy class. I've updated my question with some more info. – Zach Lysobey Jun 4 '12 at 21:26
up vote 0 down vote accepted

After you create a custom loop, you need to restore the global post variables with wp_reset_query().

share|improve this answer
I'm giving you the check, since this is the right answer. I was using wp_reset_query() but somehow must've been doing something else wrong... – Zach Lysobey Jun 26 '12 at 13:31

Ok, I sorted this out myself. Instead of get_posts or WP_Query I used a custom select query as explained here

My revised code:

<select name="<?php $mb->the_name(); ?>">
    global $wpdb;
    $selected = ' selected="selected"';
    $querystr = "
       SELECT $wpdb->posts.post_title,  $wpdb->posts.ID
       FROM $wpdb->posts
       WHERE $wpdb->posts.post_type = 'projects' 
       AND $wpdb->posts.post_status = 'publish' 
       ORDER BY $wpdb->posts.post_name DESC
    $assocProjQuery = $wpdb->get_results($querystr, OBJECT);
    foreach($assocProjQuery as $ap):
        echo '<option value="' . $ap->ID . '"';
            if ($mb->get_the_value() == $ap->ID ) echo $selected;
        echo '>' . $ap->post_title . '</option>' . "\n";
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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