WP税查询&按税排序查询不起作用

时间:2017-07-10 作者:user7193478

我有子类别(自定义分类法)。我想在一个表中获取该类别的所有产品&;然后是其他类别名称;这是桌子。为了实现这一点,我按&;分组;按自定义分类法排序。此外,我想将父类别(自定义分类法)id传递给查询。但当我一次通过这两个条件时,不会返回任何数据。

/* to order by taxonomy*/
function orderby_tax_clauses( $clauses, $wp_query ) {
global $wpdb;

if ( isset( $wp_query->query[\'orderby\'] ) && \'product_category\' == $wp_query->query[\'orderby\'] ) {

    $clauses[\'join\'] .= <<<SQL
    LEFT OUTER JOIN {$wpdb->term_relationships} ON {$wpdb->posts}.ID={$wpdb->term_relationships}.object_id
    LEFT OUTER JOIN {$wpdb->term_taxonomy} USING (term_taxonomy_id)
    LEFT OUTER JOIN {$wpdb->terms} USING (term_id)
    SQL;

    $clauses[\'where\'] .= " AND (taxonomy = \'product_category\' OR taxonomy IS NULL)";
    $clauses[\'groupby\'] = "object_id";
    $clauses[\'orderby\']  = "GROUP_CONCAT({$wpdb->terms}.name ORDER BY name ASC) ";
    $clauses[\'orderby\'] .= ( \'ASC\' == strtoupper( $wp_query->get(\'order\') ) ) ? \'ASC\' : \'DESC\';
}

return $clauses;
}
以下是我获取产品的功能:

 add_shortcode(\'product-list\',\'get_product_list\');
 function get_product_list($atts)
 {
   extract($atts = shortcode_atts(
     array(
    \'cat\' => \'\',
   ),
   $atts
  ));

global $post;
global $wp_query;



$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
add_filter( \'posts_clauses\', \'orderby_tax_clauses\', 10, 2 );
$args = array(
    \'post_type\' => \'product\',
    \'posts_per_page\' => 50,
            \'orderby\'        => \'product_category\',
            \'order\'       => \'ASC\',
    \'paged\' => $paged
);


if($atts[\'cat\'] != "") { 
    $args[\'tax_query\' ][] =array(
            \'taxonomy\' => \'product_category\',
            \'field\' => \'id\',
            \'terms\' => $atts[\'cat\'],
        );
}

$wp_query = new WP_Query( $args );
echo $wp_query->request;
//echo \'********\'.$wp_query->last_query; 
remove_filter( \'posts_clauses\', \'orderby_tax_clauses\', 10, 2 ); 

if ( $wp_query->have_posts() ) :
/* my code*/
endif;
}
我调试了(&A);找到此查询:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
LEFT OUTER JOIN wp_term_relationships ON wp_posts.ID=wp_term_relationships.object_id
LEFT OUTER JOIN wp_term_taxonomy USING (term_taxonomy_id)
LEFT OUTER JOIN wp_terms USING (term_id)
WHERE 1=1
  AND (wp_term_relationships.term_taxonomy_id IN (65))
  AND wp_posts.post_type = \'product\'
  AND (wp_posts.post_status = \'publish\'
       OR wp_posts.post_status = \'acf-disabled\')
  AND (taxonomy = \'product_category\'
       OR taxonomy IS NULL)
GROUP BY object_id
ORDER BY GROUP_CONCAT(wp_terms.name
                      ORDER BY name ASC) ASC
LIMIT 0,
      50

1 个回复
SO网友:Melissa Freeman

下面是working tax\\u查询的一个示例。

我在使用tax\\u query时遇到的问题是忘记嵌套数组(即“tax\\u query”是array(array(…))。如果你不这样做,那就行不通了。

$args = array(
    \'post_type\' => $post_type,
    \'posts_per_page\' => -1,  //show all posts
    \'tax_query\' => array(
         array(
            \'taxonomy\' => $taxonomy,
             //\'field\' => \'slug\',
             //\'terms\' => $term->slug,
             \'field\' => \'term_id\',
             \'terms\' => array([your values]),
          )
     )
 );

结束

相关推荐

POST获取忽略SORT_COLUMN?

我正在为我的投资组合使用下面的子页面代码。出于某种原因,它似乎忽略了我在页面上设置的顺序。有什么帮助吗? <?php $args = array( \'post_type\' => \'page\', \'sort_column\' => \'menu_order\', \'post_status\' => \'publish\', \'posts_per_page\