Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to display a dropdown select metabox on the "add new" page of a custom post type which displays a drop down list of titles from a separate custom post type ( it's an effort to relate the two CPT's together - one being "clients" and the other being "projects" so that when creating a new project one would be required to select a client from the drop down select ).

All works well except for the fact that somehow the query is affecting WordPress because when i click "add new project" instead of an empty field for the title like you'd normally get it is populated instead with the title of the first CLIENT. I thought that by adding wp_reset_postdata() that it would solve the problem but it didn't. Can anyone help? ( I am only adding what i think is the relevant/problematic piece of code as the metabox code itself works as expected as far as i can see and i assume the problem is with the way i've done the query)

  <select name="my_meta_box_select" id="my_meta_box_select">
  <?php 
  $my_loop = new WP_Query( array( 'post_type' => 'clients', 'posts_per_page' => 10 ) );
  while ( $my_loop->have_posts() ) : $my_loop->the_post();
  $title = get_the_title();
  ?>
  <option value="<?php echo $title ?>" <?php selected( $selected, $title ); ?>><?php echo $title ?></option>
  <?php
  endwhile;
  ?>            
  </select>

  <?php
  wp_reset_postdata();
  }
share|improve this question
    
You may be interesteed in the Posts-2-Posts plugin wordpress.org/extend/plugins/posts-to-posts –  v0idless Nov 4 '11 at 14:43

2 Answers 2

up vote 1 down vote accepted

What I would do is just create a custom taxonomy type to share between both custom post types that are dynamically created when a new post of the Clients type is created with the same title, then you can associate them without any custom meta handling.

share|improve this answer
    
i was going for custom meta over taxonomy because i thought that in general for unique values you'd use custom meta - so for example a project can have one and only one value for client. It seems like this custom meta vs taxonomy issue comes up all the time - i thought i had it sorted with that view - am i wrong –  byronyasgur Nov 4 '11 at 17:47
    
Yea, while that is true that you would use custom meta for new data, with what you're describing doing could easily be done programmatically to where it would be a better option than custom meta, in my personal opinion. –  Kevin Langley Jr. Nov 4 '11 at 18:08
    
will think about it - thanks –  byronyasgur Nov 4 '11 at 21:04

i think you need to replace

wp_reset_postdata();

with

wp_reset_query();

at least that works for me in a quick front-end test

share|improve this answer
    
WP_Query doesn't affect the $wp_query global, so you shouldn't need to reset the query. –  v0idless Nov 4 '11 at 17:39
    
only just got to try this - it didnt have any effect. thanks though –  byronyasgur Nov 5 '11 at 16:02
    
sucks, don't know what is so different about the backend. my other suggestion would be to use get_posts() which i have definitely used before to create drop downs in metaboxes. codex.wordpress.org/Template_Tags/… –  helgatheviking Nov 5 '11 at 17:44

Your Answer

 
discard

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.