How can I get the entire post of a taxonomy category by taxonomy category id or taxonomy category name in wordPress?
I created a post product type and also created a taxonomy called productcategory. Now I need to get a specific category in related products. Somehow I managed to find the id and name of the taxonomy category using
$term_list = wp_get_post_terms($post->ID, 'productcategory', array("fields" => "ids"));
Now how can I get all posts of this taxonomy category?
+3
source to share
2 answers
Show posts related to a specific taxonomy. If you specified a taxonomy registered with a custom post type, you would use "{custom_taxonomy_name}" instead of using "category". For example, if you had a common taxonomy called "genre" and wanted to show only entries from the "jazz" genre, you would use the code below.
<?php
$args = array(
'posts_per_page' => 8,
'orderby' => 'rand',
'post_type' => 'products',
'productcategory' => 'your_cat_name',
'post_status' => 'publish'
);
$show_albums = get_posts( $args );
?>
+2
source to share
$terms = get_the_terms( $post->ID, 'productcategory' );
$args = array(
'post_type' => 'products',
'tax_query' => array(
array(
'taxonomy' => 'productcategory',
'field' => 'slug',
'terms' => explode(',',$terms),
),
),
);
$query = new WP_Query( $args );
Then just start a loop using the results of that query and you should be good to go!
+1
source to share