WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm currently working on a Magazine style website and on the main page I have three different sections where I display the latest news. Slider, latest news and more news.

The problem is I can't figure out how to avoid duplicate posts on each one.

Here is a sample of the query:

// build query
query_posts( $this->getQueryParams($type, $esc_category, $this->escapeText($data['number_of_posts'])) );
// loop
while (have_posts()) : the_post();

Each section is called as a "Module" so this same code applies for the rest of the queries.


share|improve this question

You can use a static class property to store a rolling list of posts that are used in previous queries. This method allows you to pass data between classes (widgets, modules, plugins, etc...), but will also work for a procedural workflow. Everything on the page that uses your intermediary class will know what posts to exclude.

Step 1: Create a class that has a static $posts property

You will need getter and setter methods.

class RollingPostsIndex{
    public static $posts = array();

    public function set($new_posts){
        $previous_posts = self::get();
        $posts = array_push($previous_posts, $new_posts);
        return self::$posts = $posts;

    public function get(){
        return self::$posts;

Step 2: Create two helper functions to for setting and getting.

function add_rolling_posts($post_ids);

function get_rolling_posts(){
    return RollingPostsIndex::get();

Step 3: Track your post ids within your WP_Query instance loops

$post_ids[] = $post->ID;

Step 4: Add the new section post IDs array to the RollingPostsIndex static property

Use the API function we created earlier.


Step 4: Add an argument to your WP_Query instance to exclude previously used post ids.

$args['__post_not_in'] = get_rolling_posts();

I've tested this method extensively, and it works really well to keep track of post ids throughout the entire page load.

Hope this helps you out!

share|improve this answer
The file where all the queries are is called "Modules.php". From within a page I can call each individual "module" onto the index page. – Martin Hasan Aug 4 '12 at 19:36
@MartinHasan Is each 'module' in a function? – Brian Fegter Aug 4 '12 at 19:39
Exactly. It's all in one file. – Martin Hasan Aug 4 '12 at 19:52
Are you able to edit the modules.php file? – Brian Fegter Aug 4 '12 at 19:53
Yeah, I've tried a couple of thing with no success. – Martin Hasan Aug 4 '12 at 19:56

First, you should be using WP_Query rather than query_posts for these queries. query_posts is for altering the main query, but even in that case there are better methods.

Refer to WP_Query Post and Page parameters to see how to exclude posts from queries:

$id_to_exclude = 42;

$args = array(
    'post__not_in' => array( $id_to_exclude )
$results = new WP_Query( $args );
share|improve this answer
The thing is I'm working off a theme, that's why I can't go back and edit the entire code. – Martin Hasan Aug 4 '12 at 19:26

Your Answer


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.