0

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.

1 Answer 1

1

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 )
    ) 
);
3
  • examples, also to exclude multiple posts, are in codex.wordpress.org/The_Loop#Multiple_Loops
    – Michael
    Commented May 20, 2012 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
    Commented May 20, 2012 at 16:14
  • @Cerere - see edit above
    – Milo
    Commented May 20, 2012 at 16:34

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.