4

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!

4
  • Add another array in 'tax_query' => array( this should solve your problem.
    – SLH
    Nov 9, 2014 at 21:13
  • @SLH yes but what about $terms = wp_get_post_terms( $post->ID, 'products-category' ); as this is only using the product-category taxonomy term? Nov 9, 2014 at 21:29
  • Then create other variable $space_terms and get the terms by post ID for your second taxonomy. array( 'taxonomy' => 'space', 'field' => 'slug', 'terms' => $space_terms, ),
    – SLH
    Nov 9, 2014 at 21:32
  • @SLH ok, unless its something I'm doing wrong...would you possibly be able to put the full code in an answer so I can test? Nov 9, 2014 at 21:51

2 Answers 2

16

First of all, get all term slugs from the custom taxonomy space by current post ID.

$space_terms = wp_get_post_terms( $post->ID, 'space' );
if( $space_terms ) {
  $space_terms = array();
  foreach( $space_terms as $term ) {
   $space_terms[] = $term->slug;
  }
}

You should specify the logical relationship between each inner taxonomy array when there is more than one.

The relation key in the array describes the relationship. Possible values are OR and AND.

'tax_query' => array(
    'relation' => 'OR',
    array(
        'taxonomy' => 'products-category',
        'field'    => 'slug',
        'terms'    => $course_terms,
    ),
    array(
        'taxonomy' => 'space',
        'field'    => 'slug',
        'terms'    => $space_terms,
    ),
),
2

Simply get all terms first: Here i used property custom post type for example

$property_statu_terms = wp_get_post_terms( $post->ID, 'property_status', array(  'fields' => 'ids' ));
$property_type_terms = wp_get_post_terms( $post->ID, 'property_type', array(  'fields' => 'ids' ));
$property_city_terms = wp_get_post_terms( $post->ID, 'property_city', array(  'fields' => 'ids' ));
$property_state_terms = wp_get_post_terms( $post->ID, 'property_state', array(  'fields' => 'ids' ));
$property_feature_terms = wp_get_post_terms( $post->ID, 'property_feature', array(  'fields' => 'ids' ));

Use relation [OR] or [AND] to get your desired posts.

$query = new WP_Query( array(

    'post_type'             => 'property',
    'posts_per_page'        => 3,
    'orderby'               => 'rand',
    'post__not_in'          => array( $post->ID ),
    'ignore_sticky_posts'   => 1,
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'property_status',
            'field'    => 'id',
            'terms'    => $property_statu_terms,
        ),
        array(
            'taxonomy' => 'property_type',
            'field'    => 'id',
            'terms'    => $property_type_terms,
        ),
        array(
            'taxonomy' => 'property_city',
            'field'    => 'id',
            'terms'    => $property_city_terms,
        ),
        array(
            'taxonomy' => 'property_state',
            'field'    => 'id',
            'terms'    => $property_state_terms,
        ),
        array(
            'taxonomy' => 'property_feature',
            'field'    => 'id',
            'terms'    => $property_feature_terms,
        ),
    ),

));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.