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

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.

1 Answer 1


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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