Wordpress: delete the duplicate posts in multiple loops

Thursday, July 2, 2009

The useful function query_posts() or object WP_Query (which then are basically the same thing) Wordpress allows you to obtain a list of posts set many search parameters (see Very short snippet: view a list of posts by category or tag ). Precisely because of its versatility in the research of post can happen that the function using two or more times in a row you get the "duplicate". This can happen, for example, when you search (filters) for TAGs, a post, in fact, can (indeed has, in most cases) have associated tags. It follows that the list of posts with TAG A may also submit a post in the list of posts with TAG B

To eliminate the problem may proceed in two different ways, depending on the version of WordPress. The first is to put aside all the IDs of the first loop so as to exclude them in the second. This technique can be used for any number of loops and to the advantage of being compatible with older versions of WordPress:

PHP:
  1. / / First loop
  2. / / The array $ ids [] below is used to keep track of
  3. / / Id of the post that I went out with the first loop
  4. array ( ) ; $ ids = array ();
  5. ) ; query_posts ( 'category_name = Flash & showposts = 10');
  6. have_posts ( ) ) : the_post ( ) ; while (have_posts ()): the_post ();
  7. = $post -> ID ; $ ids [] = $ post -> ID;
  8. / / ... output
  9. endwhile;
  10. / / Second loop
  11. / / In this case the output is only thing
  12. / / $ Post-> ID is not present in the $ ids []
  13. ) ; query_posts ( 'category_name = Adobe & showposts = 10');
  14. have_posts ( ) ) : the_post ( ) ; while (have_posts ()): the_post ();
  15. ! in_array ( $post -> ID , $ids ) ) { if (in_array ($ post -> ID, $ ids)) (
  16. / / ... output
  17. )
  18. endwhile;

If the release of WordPress that we are using is 2.6 or higher, we can apply a variant comodoa the second loop:

PHP:
  1. / / Second loop
  2. / / WordPress 2.6 +
  3. / / In this case it is directly query_posts () to
  4. / / Exclude the IDs of the posts already displayed
  5. query_posts (
  6. 'showposts' => 10 , array ( 'showposts' => 10,
  7. 4 , 'cat' => 4,
  8. $ids , 'post__not_in' => $ ids,
  9. ));
  10. have_posts ( ) ) : the_post ( ) ; while (have_posts ()): the_post ();
  11. / / ... output
  12. endwhile;

Option post__not_in with others is well documented here.

Related Post

Was this article helpful?: Per nientePocoAbbastanzaMoltoMoltissimo
Loading ... Loading ...

3 comments to "Wordpress: eliminate duplicate posts in multiple loops"

  1. getAvatar 1.0
    02 lug, 2009 Undolog.com »Wordpress: eliminate duplicate posts in multiple loops:

    [...] Original article: Undolog.com »Wordpress: eliminate duplicate posts in multiple loops Related articles: Undolog.com" Very short snippet: Wordpress, soon on the [...]

  2. getAvatar 1.0
    04 lug, 2009 Wordpress: delete the duplicate posts in multiple loops:

    [...] Source: Wordpress: eliminate duplicate posts in multiple loops Related articles: Undolog.com »Wordpress: eliminate duplicate posts in the loop [...]

  3. getAvatar 1.0
    04 lug, 2009 Pick of the Week # 24 | BigThink:

    [...] Wordpress: delete the duplicate posts in multiple loops A little trick to avoid repeating the post when we have two loops on the same page. [...]

Leave a comment

TAG XHTML PERMISSIONS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre>         // blocco generico [code][/code]       // blocco generico [as][/as]           // Actionscript [css][/css]         // CSS Style Sheet [html][/html]       // HTML [js][/js]           // Javascript [objc][/objc]       // Objective-C [php][/php]         // PHP [sql][/sql]         // SQL