Get products with specific attribute term and specific category term in Woocommerce using SQL

Cheerychops

I wish to find products with two different criteria.

The code I used first to search one criteria is;

SELECT rel.object_id, rel.term_taxonomy_id, tt.taxonomy, tt.term_id, ts.name
FROM df1wrmw_term_taxonomy tt
INNER JOIN df1wrmw_term_relationships rel ON tt.term_taxonomy_id = rel.term_taxonomy_id
INNER JOIN df1wrmw_terms ts ON tt.term_id = ts.term_id
WHERE tt.taxonomy = "pa_1_scale"
AND ts.term_id = 400;

This returns all those products (Object_ID) with the attribute "pa_1_scale" and ts.term_id = 400. I can also do this to return all products with product_cat and ts.term_id = 397, using a different WHERE statement

WHERE tt.taxonomy = "product_cat"
AND ts.term_id = 397

UNION ALL just combines the two. How do I get SQL to select both these criteria? I know a WHERE statement combining the two criteria will not work as I think that no table row contains both values?

Any help available would be great.

LoicTheAztec

You can try to use the following that will join duplicated tables with a different variable reference, allowing to combine both queries in one:

SELECT tr.object_id, tr.term_taxonomy_id,  tt.taxonomy,  t.term_id,  t.name,
    tr2.term_taxonomy_id as term_taxonomy_id2, tt2.taxonomy as taxonomy2, 
    t2.term_id as term_id2, t2.name as name2
FROM df1wrmw_term_relationships tr
INNER JOIN df1wrmw_term_taxonomy tt
    ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN df1wrmw_terms t
    ON tt.term_id = t.term_id
INNER JOIN df1wrmw_term_relationships tr2
    ON tr.object_id = tr2.object_id
INNER JOIN df1wrmw_term_taxonomy tt2
    ON tr2.term_taxonomy_id = tt2.term_taxonomy_id
INNER JOIN df1wrmw_terms t2
    ON tt2.term_id = t2.term_id
WHERE tt.taxonomy = 'pa_1_scale'
    AND t.term_id = 400
    AND tt2.taxonomy = 'product_cat'
    AND t2.term_id = 397

Or you can use in WordPress the class WPDB and its methods to get SQL query results in PHP:

global $wpdb;

$results = $wpdb->get_results("
    SELECT tr.object_id, tr.term_taxonomy_id,  tt.taxonomy,  t.term_id,  t.name,
        tr2.term_taxonomy_id as term_taxonomy_id2, tt2.taxonomy as taxonomy2, 
        t2.term_id as term_id2, t2.name as name2
    FROM {$wpdb->prefix}term_relationships tr
    INNER JOIN {$wpdb->prefix}term_taxonomy tt
        ON tr.term_taxonomy_id = tt.term_taxonomy_id
    INNER JOIN {$wpdb->prefix}terms t
        ON tt.term_id = t.term_id
    INNER JOIN {$wpdb->prefix}term_relationships tr2
        ON tr.object_id = tr2.object_id
    INNER JOIN {$wpdb->prefix}term_taxonomy tt2
        ON tr2.term_taxonomy_id = tt2.term_taxonomy_id
    INNER JOIN {$wpdb->prefix}terms t2
        ON tt2.term_id = t2.term_id
    WHERE tt.taxonomy = 'pa_1_scale'
        AND t.term_id = 400
        AND tt2.taxonomy = 'product_cat'
        AND t2.term_id = 397
");

// Display preformatted raw output
echo '<pre>' . print_pr($results, true) . '</pre>';

It should work.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Auto set specific attribute term value to purchased products on Woocommerce

Get a product from specific product attribute term name in WooCommerce

Get term names array from all product for a specific product attribute in Woocommerce

Woocommerce - get product in category with specific attribute

Display a list of products that belong to a specific term

Get all Woocommerce products from current product category term Id in a WP_Query

Get specific product attribute values for a specific product category in Woocommerce

Get all WooCommerce products from a category that have specific meta data

how can i get products for specific category from woocommerce restapi using php

Woocommerce MySQL to update price of products in specific category

Hide products having a specific product category in Woocommerce

Display products from specific product category in Woocommerce

Display with a shortcode a dropdown of Woocommerce products for a specific category

Apply discount on products within specific category (WooCommerce)

Make order notes field required for variations with specific product attribute term in WooCommerce

WooCommerce: Get total of out of stock products by taxonomy term

Using Laravel How can get a specific sizes products for a category?

Get a list of siblings term ids from current product category in WooCommerce

Get only one product category term for a WooCommerce product

Specific term for "IS" function

Retrieve specific record by term

Get products from a specific brand in WooCommerce using a WP_Query

by clicking category get the specific products in php

How can i get Products by specific category

Hide attribute dropdown for a specific WooCommerce product category

Use quanteda to calculate term specific term and inverse term frq

Search products in a specific category

How to translate WooCommerce term 'Products' to 'Properties'

Set a product category term in a product on Woocommerce