Title: Help with query_posts()
Last modified: August 20, 2016

---

# Help with query_posts()

 *  Resolved [AndrewRO](https://wordpress.org/support/users/andrewro/)
 * (@andrewro)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/help-with-query_posts-5/)
 * So I have this little piece of code that retrieves random posts from the database
   in order to display them in the sidebar (widget).
 *     ```
       $args = array(
       		'posts_per_page' => $show_count,
       		'post_type' => 'post',
       		'post_status' => 'publish',
       		'orderby' => 'rand'
       	);
       query_posts($args);
       ```
   
 * I need a little bit of help editing it so that it wouldn’t select one of the 
   10 latest posts (that are shown on the front page) nor the current post (for 
   example if a user is reading an article, that article id to be excluded from 
   this query so there is no chance that the widget will output the same article
   in the ‘Random Post’ box).

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/help-with-query_posts-5/#post-3292766)
 * you might need to use `get_posts()` to grap the latest 10 posts, and use that
   together with the current `$post->ID` to exclude them from your query;
 * also do not use `query_posts()` for secondary loops – use `WP_Query()` instead(
   [http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts?rq=1](http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts?rq=1)).
 * example:
 *     ```
       global $post;
       $latest = get_posts('posts_per_page=10');
       $latest_ids = array();
       if( $latest ) foreach( $latest as $late ) { $latest_ids[] = $late->ID; }
       $exclude = array_merge( $latest_ids, array( $post->ID ) );
   
       $args = array(
       		'posts_per_page' => $show_count,
       		'post_type' => 'post',
       		'post_status' => 'publish',
       		'orderby' => 'rand',
       		'post__not_in' => $exclude
       	);
       $random = new WP_Query($args);
       if( $random->have_posts() ) while( $random->have_posts() ) : $random->the_post();
       /* here we go loopy-loo */
       endwhile; wp_reset_postdata();
       ```
   
 * (untested)
 * [http://codex.wordpress.org/Class_Reference/WP_Query](http://codex.wordpress.org/Class_Reference/WP_Query)
 *  Thread Starter [AndrewRO](https://wordpress.org/support/users/andrewro/)
 * (@andrewro)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/help-with-query_posts-5/#post-3292780)
 * Thanks alchymyth!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Help with query_posts()’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [AndrewRO](https://wordpress.org/support/users/andrewro/)
 * Last activity: [13 years, 6 months ago](https://wordpress.org/support/topic/help-with-query_posts-5/#post-3292780)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
