Get Woocommerce products by custom taxonomy term values in a WP_Query

12,922

You should try the following WP_Query $args that will allow to get 2 other products from the same "collane" custom taxonomy that have the same terms than the current product.

I am using wp_get_post_terms() WordPress function to get the terms IDs in a post (here "product" custom post type) from a specific custom taxonomy.

The code:

$taxonomy = 'collane'; // The targeted custom taxonomy

// Get the terms IDs for the current product related to 'collane' custom taxonomy
$term_ids = wp_get_post_terms( get_the_id(), $taxonomy, array('fields' => 'ids') ); // array

$query = new WP_Query( $args = array(
    'post_type'             => 'product',
    'post_status'           => 'publish',
    'ignore_sticky_posts'   => 1,
    'posts_per_page'        => 2, // Limit: two products
    'post__not_in'          => array( get_the_id() ), // Excluding current product
    'tax_query'             => array( array(
        'taxonomy'      => $taxonomy,
        'field'         => 'term_id', // can be 'term_id', 'slug' or 'name'
        'terms'         => $term_ids,
    ), ),
);

// Test count post output
echo '<p>Posts count: ' . $query->post_count . '</p>';

// The WP_Query loop
if ( $query->have_posts() ): 
    while( $query->have_posts() ): 
        $query->the_post();

        // Test output
        echo '<p>' . $query->post->post_title . ' (' . $query->post->ID . ')</p>';

    endwhile;
    wp_reset_postdata();
endif;
Share:
12,922
Kryuko
Author by

Kryuko

Updated on June 04, 2022

Comments

  • Kryuko
    Kryuko almost 2 years

    I'm developing a plugin that will be placed in my woocommerce product sidebar. I need that, given the product id/object, it will find 2 product with the same custom taxonomy I created before.

    With this code I got the list of terms used in my product where "collane" is the custom taxonomy:

    get_the_term_list( $product->id, 'collane', '<div style="direction:rtl;">', '</div>', '' ); 
    

    The problem is that I don't know how to get the custom taxonomy id, and how to filter it by my custom taxonomy.

    I've already used WP_Query to find products in the same category with this code:

    $args = array(
    'post_type'             => 'product',
    'post_status'           => 'publish',
    'ignore_sticky_posts'   => 1,
    'posts_per_page'        => $atts['limit'],
    'tax_query'             => array(
        array(
            'taxonomy'      => 'product_cat',
            'field' => 'term_id', //This is optional, as it defaults to 'term_id'
            'terms'         => $cat_id,
            'operator'      => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
        ),
        array(
            'taxonomy'      => 'product_visibility',
            'field'         => 'slug',
            'terms'         => 'exclude-from-catalog', // Possibly 'exclude-from-search' too
            'operator'      => 'NOT IN'
        )
    )
    );
    

    How can I change my code to obtain the wanted taxonomy id/object, and then use it in my WP_Query?

  • Kryuko
    Kryuko about 6 years
    Thank you, i'm testing it. For now i can tell that in your code, one bracket is missing and the code can't run.