I'm trying to retrieve the posts of specific taxonomy categories. I have two taxonomy categories.
- Category One
- 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.
WP_Query
. Query insideforeach
loop? Very.. bad.. idea. You can easily make just one query which would also eliminate double posts problem. Take a look at the codex.