Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Given a blog featuring several categories (such as "Sport", "Nature", etc), I've then proceded to create a custom taxonomy, which allows me to show articles in certain areas of my blog with terms such as "slideshow", "sidebar-highlight", "top-category", etc.

Inside my sidebar, I've created an area where articles (from the same category, lets say "Nature") are shown using two different loops. The first one shows only one article (the first one in chronological order, from the nature category and which is also inside the "sidebar-highlight" taxonomy's term) with a featured image, title and excerpt, whilst the second, only shows the latest 3 articles from the Nature category. In order to avoid duplicates, I've been using the following code:

$nature_loop_1 = new WP_Query( array (

    'category_name' => 'nature', 
    'tax_query' => array (
        array (
            'taxonomy'  => 'highlight',
            'field'     => 'slug', 
            'terms'     => 'sidebar-highlight',
            'operator'  => 'IN' 
        ) 
    ),
    ) 
 );
$nature_loop_2 = new WP_Query( array ( 

    'category_name' => 'nature', 
    'tax_query' => array (
        array (
            'taxonomy'  => 'highlight',
            'field'     => 'slug', 
            'terms'     => 'sidebar-highlight',
            'operator'  => 'NOT IN' 
        ) 
    ),
    ) 
 ); 

Yet, while this works, this means articles from "sidebar-highlight" will never show inside the second loop, no matter when they were posted. So I was wondering if is there any way I could manage to avoiding duplicates while allowing articles from "sidebar-highlight" term show.

Thanks in advance.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Pass the post ID from the first query as a post__not_in parameter to exclude it from the second query.

$nature_loop_1 = new WP_Query(
    array (
        'category_name' => 'nature', 
        'tax_query' => array (
            array (
                'taxonomy'  => 'highlight',
                'field'     => 'slug', 
                'terms'     => 'sidebar-highlight',
                'operator'  => 'IN' 
            ) 
        ),
    ) 
);

$exclude = $nature_loop_1->post->ID;

$nature_loop_2 = new WP_Query(
    array ( 
        'category_name' => 'nature', 
        'post__not_in' => array( $exclude )
    ) 
);
share|improve this answer
    
examples, also to exclude multiple posts, are in codex.wordpress.org/The_Loop#Multiple_Loops –  Michael May 20 '12 at 15:36
    
I'm afraid I've lost you there Milo: could you spare a few more minutes in order to provide me with an actual example of how I should actually write down the two loops? As for the codex link you game me Michael, while I tank you for looking into it, I've already gone through those pages and few more tutorials, yet I didn't manage to get anything to work properly, both I guess because I lack a good PHP knowledge and because every example/tutorial I found, discussed about avoiding duplicates between categories, but there were no reference regarding taxonomies and terms. –  Cerere May 20 '12 at 16:14
    
@Cerere - see edit above –  Milo May 20 '12 at 16:34
    
It works perfectly, thanks a lot! :D –  Cerere May 20 '12 at 18:29

Your Answer

 
discard

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.