WooCommerce WP_QUERY USING TAX_QUERY不返回任何结果(0=1)

时间:2020-07-26 作者:kernel52

我正试图根据产品属性(分类法)从WooCommerce获取特定产品。

$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,
                        ),
            
            ),
    
    
        );
从SQL的角度来看,我可以看到:

SELECT * FROM `wp_terms` WHERE term_id = 891

退货

wp_terms

SELECT * FROM `wp_term_taxonomy` where term_id = 891

退货enter image description here

SELECT * FROM `wp_term_relationships` where term_taxonomy_id = 891

退货

wp_term_relationships

虽然至少有一个产品添加了分类法,但由于内部查询返回(0=1),我一直收到0个结果,这意味着它找不到任何具有此分类法属性的产品。

我能做些什么来克服这个问题?

P、 S.完整功能

$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;
};

1 个回复
SO网友:kernel52

Plugin的公司提出了一个使用wpdb对象并手动编写查询的解决方案。。。荣誉归于@Sally CJ. 您的理论是正确的,因为在执行脚本时,分类法还不知道(或注册)。由于时间有限,我现在将采用这种方法,但如果我找到最初问题的解决方案,我将尝试回去。

相关推荐

Filter Custom Taxonomy Posts

我有一个自定义的分类页面,它也有一个向上的滑块。问题是滑块显示来自所有分类法而非当前分类法的随机帖子。是否有办法过滤滑块中的图像,使其仅使用当前分类法?<?php global $taxonomy_location_url, $taxonomy_profile_url; $args = array( \'post_type\' => $taxonomy_profile_url, \'orderby\' => \'rand\', \'meta_que