0

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' );
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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