0

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.

Thanks!

2 Answers 2

3

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);
    RollingPostsIndex::set($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.

add_rolling_posts($post_ids);

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!

10
  • 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. Aug 4, 2012 at 19:36
  • @MartinHasan Is each 'module' in a function? Aug 4, 2012 at 19:39
  • Exactly. It's all in one file. Aug 4, 2012 at 19:52
  • Are you able to edit the modules.php file? Aug 4, 2012 at 19:53
  • Yeah, I've tried a couple of thing with no success. Aug 4, 2012 at 19:56
1

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 );
1
  • The thing is I'm working off a theme, that's why I can't go back and edit the entire code. Aug 4, 2012 at 19:26

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.