1

I'm trying to retrieve the posts of specific taxonomy categories. I have two taxonomy categories.

  1. Category One
  2. Category Two

In these two have some different and same posts. So when i retrieve the data through terms id then query return some post two times, because these posts are link from above two categories.

$ourwork_cat_ids = array(3,4);// Category Term ID
$args_OW = array();
foreach($ourwork_cat_ids as $wIds){
    $args_OW[] = array(
    'post_type' => 'portfolio',
    'tax_query' => array(
        array(
            'taxonomy' => 'portfolio_category',
            'field' => 'id',
            'terms' => $wIds
         )
      )
    );
}//end foreach
foreach($args_OW as $OW){
    $portfolioQuery = new WP_Query($OW);
    if( $portfolioQuery->have_posts() ){ 
            while( $portfolioQuery->have_posts() ){ $portfolioQuery->the_post(); 
                echo the_title().'<br />';
            }
    }

}// end foreach

 Out Put
post one 
post two 
post two
post three

So is there possible that i can skip double post ? I will appreciate if someone guide me for that. Thanks.

2
  • You're misusing WP_Query. Query inside foreach loop? Very.. bad.. idea. You can easily make just one query which would also eliminate double posts problem. Take a look at the codex.
    – N00b
    Commented Feb 18, 2016 at 12:06
  • @N00b do you know i'm using custom taxonomy and are you sure this method will work on custom taxonomy category ? Commented Feb 18, 2016 at 12:13

2 Answers 2

2

You should never use WP_Query inside that kind of loop (there might be few exceptions but not in your case) because you can do most things with one query. This codex link has everything related to WP_Query with explanations and very simple examples.

If taxonomies were a list of groceries and a trip to shop would be a query I doubt you would go to shop for each item separately. No, you will go once and buy them all.


Take a look at these examples and let me know if you're having any problems.

//If you're using actual categories
$args = array(
    'post_type' => 'portfolio',
    'cat' => '2, 4, 5, 77, 1031' // Category IDs
);

//If you're using single custom taxonomy
$args = array(
    'post_type' => 'portfolio',
    'tax_query' => array(
        array(
            'taxonomy' => 'portfolio_tax',
            'field'    => 'term_id',
            'terms'    => array( 2, 4, 5, 77, 1031 ) // Term IDs
        )
    )
);

//If you're using multiple custom taxonomies
$args = array(
    'post_type' => 'portfolio',
    'tax_query' => array(
        'relation' => 'AND',  // Relation can be 'AND' or 'OR'
        array(
            'taxonomy' => 'portfolio_tax',
            'field'    => 'term_id',
            'terms'    => array( 2, 4, 5, 77, 1031 ) // Term IDs
        ),
        array(
            'taxonomy' => 'second_tax',
            'field'    => 'term_id',
            'terms'    => array( 1, 3, 6, 81, 1251 ) // Term IDs
        )
    )
);


//Query itself with output
$query_results = new WP_Query( $args );

while( $query_results->have_posts() ) { 

    $query_results->the_post(); 
    echo the_title() . '<br />';
}
1
  • You give me great, perfect and short coded answer, I appreciate you and your knowledge. Its working fine. Commented Feb 18, 2016 at 12:37
0

You are doing it wrong. You don't need to use a foreach loop here. tax_query parameter terms can be int or string or array.

You can make your code more simple like this:

$args = array(
    'post_type' => 'portfolio',
    'tax_query' => array(
        array(
            'taxonomy' => 'portfolio_category',
            'field' => 'id',
            'terms' => array(3,4);// Category Term ID
         )
      )
    );

To skip double post, we can use posts_distinct filter. We need to put it before our WP_Query.

function search_distinct() {
    return "DISTINCT";
}
add_filter('posts_distinct', 'search_distinct');

Now, here is our WP_Query:

$portfolioQuery = new WP_Query($args);
if( $portfolioQuery->have_posts() ){ 
        while( $portfolioQuery->have_posts() ){ $portfolioQuery->the_post(); 
            echo the_title().'<br />';
        }
}
wp_reset_postdata();

We will need to remove the filter.

remove_filter('posts_distinct', 'search_distinct');
2
  • WP_Query does not return duplicate posts by default, even when you pass duplicate ID's to it. Commented Feb 18, 2016 at 12:50
  • @PieterGoosen Okay I wasn't aware of that. Then what is the purpose of posts_distinct filter? Commented Feb 18, 2016 at 12:55

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.