Posts Tagged ‘the loop’

How To: Avoid Duplicate Posts

27
responses
by
Ronald Huereca
on
May 17th, 2008
in
HOW-TO, WordPress, WordPress Hack

A reader writes in:

I’m developing a new theme and I’m having trouble getting duplicate posts from showing when running two loops (one standard loop and one from a specific category). Even when I copied the specific code from directly from the codex, it was not working.

The Codex article the reader mentioned was regarding the Loop. Although the example shows how to avoid a single duplicate post, it doesn’t show how to avoid duplicating multiple posts.
Here’s how to show two individual loops without duplicating posts in either loop.
Step 1: Add a ‘posts_where’ Function
A WordPress filter is needed to accomplish this, and we’re going to be tapping into the ‘posts_where‘ filter.
The reason being is we need to modify the query used for the loop and exclude some posts.
Here’s the function we’ll be using called post_strip:

function post_strip($where) {
global $myPosts, $wpdb;
$where .= " AND $wpdb->posts.ID not in($myPosts) ";
return $where;
}

In the [...]

[Continue Reading...]

1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 5.00 out of 5)
Loading ... Loading ...


Obviously Powered by WordPress. © 2003-2010 MidOut LLC

page counter