0
votes

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

<?php
    $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

enter image description here

1

1 Answers

0
votes

You can get multiple taxonomy terms using array in get_terms. Replace your code with below:

$args = array('post_type' => 'creditcards',
    'posts_per_page' => -1
);

$loop = new WP_Query($args);

if ($loop->have_posts()) {

    while ($loop->have_posts()) : $loop->the_post();
        $custom_terms = wp_get_post_terms(get_the_ID(), array(
            'banks',
            'joiningfees',
            'cardtype'), array("fields" => "all"));

        $custom_term = wp_list_pluck($test, 'name'); //To get term name from term taxonomy array

        echo join(', ', $custom_term); //This will print all term name and remove ',' from last term name
        ?>
        <div>
            <strong>
                <?php echo the_title(); ?>
            </strong>
        </div>
        <?php
    endwhile;
}