I have a function set up to show 'Similar Products', i.e. showing products that share the same products-category
taxonomy term. This works great but I want to narrow the function even further and make it more specific. Thus I want it to look at 2 taxonomies (product-category
and space
) to find similar products. The current markup as follows:
<?php
$terms = wp_get_post_terms( $post->ID, 'products-category' );
if($terms){
// post has course_type terms attached
$course_terms = array();
foreach ($terms as $term){
$course_terms[] = $term->slug;
}
$original_query = $wp_query;
$wp_query = null;
$wp_query = new WP_Query(
array(
'posts_per_page' => '4',
'post_type' => 'regularproducts',
'tax_query' => array(
array(
'taxonomy' => 'products-category',
'field' => 'slug',
'terms' => $course_terms, //the taxonomy terms I'd like to dynamically query
),
),
'orderby' => 'title',
'order' => 'ASC',
)
);
if ( have_posts() ): ?>
<?php while (have_posts() ) : the_post(); ?> //etc...
So it currently only looks at the products-category
taxonomy for similar products (posts), but I want it to look at BOTH product-category
and space
to display more specific similar products, if possible. Any suggestions would be greatly appreciated!
'tax_query' => array(
this should solve your problem.$terms = wp_get_post_terms( $post->ID, 'products-category' );
as this is only using theproduct-category
taxonomy term?array( 'taxonomy' => 'space', 'field' => 'slug', 'terms' => $space_terms, ),