0

I am trying to get specific Products from WooCommerce baseed on a Product Attribute (taxonomy).

$args = array(
            'post_type' => array('product', 'product_variation'),
            'post_status' => 'publish',
            'tax_query' => array(
                 array(
                       'taxonomy'         => 'pa_range-kwh',
                       'terms'            => 'k2',
                       'field'            => 'slug',
                       'operator'         => 'IN',
                       'include_children' => true,
                        ),
            
            ),
    
    
        );

From an SQL point of view I can see that:

SELECT * FROM `wp_terms` WHERE term_id = 891

returns

wp_terms

SELECT * FROM `wp_term_taxonomy` where term_id = 891

returns enter image description here

SELECT * FROM `wp_term_relationships` where term_taxonomy_id = 891

returns

wp_term_relationships

Although there is at least one product where the taxonomy is added, I keep on receiving 0 results due to the inner query returning (0 = 1), meaning it can't find any product with this taxonomy attributed.

Any ideas on what could I do to overcome this?

P.S. the complete function

$GLOBALS['SERVER_SIDE_EQUATIONS']['get_products'] = function(){

    $products = '';

 
    
    $argsAlt = array(
            'post_type' => array('product', 'product_variation'),
            'post_status' => 'publish',
            'suppress_filters' => true,
            'range-kwh' => 'k2',
            'pa_range-kwh' => 'k2',
            'lazy_load_term_meta' => false,
            'tax_query' => array(
            'relation' => 'OR',
            array(
                'taxonomy'         => 'pa_range-kwh',
                'terms'            => array('k2'),
                'field'            => 'slug',
                'operator'         => 'IN',
                'include_children' => true,
            ),
            
),//end tax_array
    
    
        );

        $queryAlt = new WP_Query( $argsAlt );



        if($queryAlt->have_posts()){
            $products .='yes ';
            $products .= $custom_posts->found_posts. "<br />";          
            $_pf = new WC_Product_Factory();
            foreach($queryAlt->posts as $post){
                $product_obj = $_pf->get_product($post->ID);
                $products .= $product_obj->get_title();
            } //end foreach
        }else{
            $products .= '<br />There are no products that satisfy the search criteria.';
        }//end if       
 
    return $products;
};
10
  • My problem is similar to this: wordpress.stackexchange.com/questions/371797/…
    – kernel52
    Jul 26, 2020 at 11:13
  • As you're proficient with MySQL, one way to debug this would be to use the Wordpress query log, or for example the Query Monitor plugin to find the SQL generated by WP_Query and then take it apart to see why it's returning 0 rows and update the query args accordingly
    – mozboz
    Jul 26, 2020 at 13:47
  • Where are you using the code, what page and where on that page, is it in a hook? And you can try adding 'suppress_filters' => true to your query args and see if that helps (i.e. whether the 0 = 1 still happening). If suppressing filters helps, i.e. the issue doesn't persist, then maybe a plugin or code is filtering your query.
    – Sally CJ
    Jul 26, 2020 at 14:22
  • thanks to both for the input. I was about to add some extra info. I am running this query through the server side scripts of CFF plugin. I did use 'suppress_filters' already but no luck In a demo page, I manage to get the result after all, not using at all the tax_query but declaring a value pair "pa_range-kwh" => 'k2' in the argument list and adding 'lazy_load_term_meta' => false. Strangely it works. I am debugging via print_r on the WP_Query object, but I don't see the taxonomy query, as I only get the result (0 = 1). Did not try yet with the query log. is my next step :)
    – kernel52
    Jul 26, 2020 at 14:43
  • is just very strange, as if the taxonomy is ignored because of the server side script, or the plugin itself
    – kernel52
    Jul 26, 2020 at 15:23

1 Answer 1

0

Plugin's company proposed a solution to go with the wpdb object and write manually the queries... Kudos to @Sally CJ. You were right in you theory, as the taxonomies are not yet known (or registered) when the script is executed. I will go with this approach for now, as time is limited, but will try to get back if I find a solution to the initial problem.

1
  • @Sally CJ gets the credit
    – kernel52
    Jul 27, 2020 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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