mercredi 25 mars 2015

Select posts with a custom taxonomy as a custom field


I am using woocommerce, so I have the product category taxonomy (product_cat). On a product category page, I would like to display the posts that are in that (product) category. That will be the announcements for that product category.


So I add a custom taxonomy field named 'related_category' to the posts and select the product_cat as the taxonomy. Here are my settings:


enter image description here


On the product category page, here is what I do:



$currentId = get_the_ID();
//Get terms
$terms = get_the_terms( $currentId, 'product_cat', 'string');
//Pluck out the IDs to get an array of IDS
$term_ids = wp_list_pluck($terms,'term_id');


At this point I have an array of id's. Then I build a query:



$r = new WP_Query( apply_filters( 'widget_posts_args', array(

'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'related_category',
'field' => 'id',
'terms' => $term_ids,
'operator'=> 'IN'
))

) ) );


And I get no results. I check the database and see that the posts have the right values in their custom fields:


enter image description here


I am suspecting that there is some mistake in my query. Any ideas how to form the query?





Aucun commentaire:

Enregistrer un commentaire