0

I have 3 different taxonomies in a custom post type. I want to print all posts with term name with them.

for example:

taxonomy 1 =>taxonomy=banks&post_type=creditcards

taxonomy 2 =>taxonomy=joiningfees&post_type=creditcards

taxonomy 3 =>taxonomy=cardtype&post_type=creditcards

So I want to print all the custom posts with the term names I am able to print data by entering single taxonomy but how to print data with all the taxonomy terms

Query

$custom_terms = get_terms('banks');

foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'creditcards',
'tax_query' => array(
       array(
             'taxonomy' => 'banks',
             'field' => 'slug',
             'terms' => $custom_term->slug,
            ),
           ),
);

$loop = new WP_Query($args);
if($loop->have_posts()) {

while($loop->have_posts()) : $loop->the_post();?

<div><?php echo $custom_term->name;?><br><?php echo the_title();?></div>
 <?php endwhile;
       }
       }
?>

End Result that i want in post loop

taxonomy1 term,taxonomy2 term,taxonomy3 term
the title

taxonomy1 term,taxonomy2 term,taxonomy3 term
the title

taxonomy1 term,taxonomy2 term,taxonomy3 term
the title
0

You can access the terms with this function. You can pass the name of the taxonomy as well as the ID to the function. Make sure to grab the ID correctly inside the loop - get_the_ID() should work. After that it's a matter of joining the results with comma separation. With array_merge you can add also additional arrays that you need.

  $x_terms = get_the_terms( get_the_ID(), 'any-taxonomy-slug');
  $y_terms = get_the_terms( get_the_ID(), 'another-taxonomy-slug');

  $terms = array_merge($x_terms, $y_terms);

  foreach($terms as $term):
     $term_names[] = $term->name;
  endforeach;

  $names = join( ", ", $term_names );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.