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 have a custom query to display random posts in my sidebar like this:

$newQuery = array(
     'post__in'  => get_option('sticky_posts'),
     'post_type' => 'ad_listing',
     'posts_per_page' => 10,
     'orderby' => 'rand',
     'post_status' => 'publish');

// Get new set of ads
$smStickyPosts = new WP_Query($newQuery);

if ($smStickyPosts->have_posts()) : while($smStickyPosts->have_posts() : $smStickyPosts->the_post();
    //Display posts
endwhile;

This works fine in all pages, except on the search page, for some reason the first post on the list is always the post that coincides with the search term (for example, if I search for foo and I have a post with the title "foo" it will show first followed by 9 random posts), it's like the search term is getting passed to the custom loop as well.

Also I noticed that if I search for something that doesn't find any results the posts are always the same, it's like the orderby => rand is not doing anything, it works fine on the rest of the site though.

Does anyone know what could be wrong?

Thanks in advance.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Suggest you have a look at http://wordpress.org/support/topic/passing-current-single_tag_title-as-variable-into-new-wp_query - I suspect the very last comment which covers both backing up and reseting your query will help.

share|improve this answer
    
Thanks @buzztone that helped! –  javiervd Apr 1 '11 at 4:36

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.