(che poi sono fondamentalmente la stessa cosa) di WordPress permette di ottenere una lista di post impostando numerosi parametri di ricerca (vedi Very short snippet: visualizzare una lista di post per categoria o tag ). The useful feature query_posts() or object WP_Query (which are basically the same thing) WordPress allows you to get a list of many post-setting search parameters (see Very short snippet: Display a list of posts by category or tags ). Precisely because of its versatility in search of post may happen that the function using two or more times in a row you get the "duplicate". This can happen, for example, when searching (filtering) for TAG, a post, in fact, may (indeed it has, in most cases) have associated tags. . It follows that the list of posts with TAG A may also post a list of posts with the TAG B .
To eliminate this problem you can 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | / / First loop / / The array $ ids [] is used below to keep track of / / Id of the post that I came out with the first loop array ( ) ; $ Ids = array (); ) ; query_posts ('category_name = Flash & showposts = 10'); have_posts ( ) ) : the_post ( ) ; while (have_posts ()): the_post (); = $post -> ID ; $ Ids [] = $ post -> ID; / / ... output endwhile; / / Second loop / / In this case the output is only thing that / / $ Post-> ID is not present in the $ ids [] ) ; query_posts ('category_name = Adobe & showposts = 10'); have_posts ( ) ) : the_post ( ) ; while (have_posts ()): the_post (); ! in_array ( $post -> ID , $ids ) ) { if ( in_array ($ post -> ID, $ ids)) { / / ... output } endwhile; |
If the release of WordPress is that we're using 2.6 or higher, we can apply a variant comodoa the second loop:
1 2 3 4 5 6 7 8 9 10 11 12 | / / Second loop / / WordPress 2.6 + / / In this case it is directly query_posts () to / / Exclude the IDs of the posts already displayed query_posts ( 'showposts' => 10 , array ('showposts' => 10, 4 , 'Cat' => 4, $ids , 'Post__not_in' => $ ids, )); have_posts ( ) ) : the_post ( ) ; while (have_posts ()): the_post (); / / ... output endwhile; |
The option post__not_in along with others is well documented here .










[...] Original Article: Undolog.com »WordPress: eliminate duplicate posts in multiple loops Related articles: Undolog.com" Very short snippet: Wordpress, soon on [...]
[...] Source: WordPress: eliminate duplicate posts in multiple loops Related articles: Undolog.com »WordPress: eliminate duplicate posts in the loop [...]
[...] WordPress: eliminate duplicate posts in multiple loops A little trick to avoid a repeat post, when we have two loops on the same page. [...]
Very interesting article, I have a question that could implement the execution of the script:
using this code, if I click on the second page, I automatically return the same after the first. How can I avoid this?
Thanks