I have a custom post type called ‘brand’, with a taxonomy of ‘brand-categories’. try as I might, I cannot find a way to display a list of posts in the current taxonomy. can anyone help?

<?php
function List_Brands_of_Category(){
$terms = wp_get_post_terms( $post->ID, 'brand-categories'); 

foreach ( $terms as $term ) {
    echo "$term->ID"; 
}
$args = array(
    'post_type' => 'brand',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'brand-categories',
            'field'    => 'ID',
            'terms'    => array($term->term_id)
        )
    ),
);// end args



$query = new WP_Query($args); ?>
    <?php       
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            echo 'Brand';
            $query->the_post(); 
        } 
    } 
    else {?>

    <p><?php _e( 'Sorry, no posts matched your criteria.' ); 
    }

    ?></p>

<?php 
}

  function DMBrandofCategory_Cleanse() {
     ob_start();
        List_Brands_of_Category();
      return ob_get_clean();
 }
 add_shortcode('WC_BOC', 'DMBrandofCategory_Cleanse');
 ?>

1 Answer
1

$args = array(
    'post_type' => 'brand',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'brand-categories',
            'field'    => 'name',
            'terms'    => get_queried_object()
        )
    ),
);// end args

Leave a Reply

Your email address will not be published. Required fields are marked *