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'm having issues with this. I have 2 while loops, each getting different custom post types. They both work fine, except, my sidebar content i.e. widgets, is not appearing. I think this is because I am not running the default query.

My two queries are:

query_posts('post_type=offered'); 
while (have_posts()) : the_post(); ?>
....content
endwhile;

and

$wanted_query = new WP_Query('post_type=wanted');
while ($wanted_query->have_posts()) : $wanted_query->the_post();
....content
endwhile;

However, when I try to combine them as below, I get the sidebar content but no my posts!

global $query_string;
query_posts( $query_string . '&post_type=offered' );
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

When you do a query, you change the environment, specifically the current post and query objects. To reset things back to how they were before the query loop you should:

query_posts('post_type=offered'); 
if(have_posts(){
    while (have_posts()) : the_post(); ?>
    ....content
    endwhile;
}
wp_reset_query(); // reset query!!!!

or

$wanted_query = new WP_Query('post_type=wanted');
if($wantedquery->have_posts()){
    while ($wanted_query->have_posts()) : $wanted_query->the_post();
    ....content
    endwhile;
}
wp_reset_postdata();

etc, also, check if you have posts before doing the loop, rather than going straight into the while loop.

share|improve this answer
    
it worked! Thanks. –  Nicola Apr 3 '12 at 17:20
add comment

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.