0
votes

Hi i am trying to display posts of a custom taxonomy based on its terms.The below code returns one after other what i need is only for one particular term. For example i have two cities under my custom taxonomy city-guide-cities they are lets say paris and newyork.I need to display only posts under paris. I tried in wpq array to pass paris as term arg it display only paris posts but the loop works. means paris posts display under first paris then under newyork.I do not want newyork to display in there or not want the loop to run once paris or newyork is loaded.

<?php
$terms = get_terms('city-guide-cities');

foreach ($terms as $term) {
$wpq = array ('taxonomy'=>'city-guide-cities','term'=>$term->slug);
$myquery = new WP_Query ($wpq);
$article_count = $myquery->post_count;
echo "<h3 class=\"term-heading\" id=\"".$term->slug."\">";
echo $term->name;
echo "</h3>";
if ($article_count) {
echo "<ul>";
while ($myquery->have_posts()) : $myquery->the_post();
  echo "<li><a href=\"".get_permalink()."\">".$post->post_title."</a></li>";
endwhile;
echo "</ul>";
}
}
?>
1
my post type is city-guide,taxonomy is city-guide-cities , term is paris , i want posts under paris - Melvin

1 Answers

0
votes

See https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters. Should work for you.

$query = new WP_Query( 
array( 
    'post_type' => 'city-guide',
    'tax_query' => array(
        array(
            'taxonomy' => 'city-guide-cities',
            'field'    => 'slug',
            'terms'    => 'paris',
        ),
    )
 ) 
);

You can also chain these, with AND or OR, to do multiple term queries as you need.