简体   繁体   English

get_terms 忽略 WooCommerce 产品类别上的 orderby 参数

[英]get_terms ignores orderby argument on WooCommerce product categories

I used the following function for listing product categories and order them by name.我使用以下功能列出产品类别并按名称排序。 Since I last updated WooCommerce from 3.5.7 to 3.6.1 and this is not working any more.自从我上次将 WooCommerce 从 3.5.7 更新到 3.6.1 以来,这不再起作用了。

It does not matter, what I write into orderby .没关系,我写什么到orderby The terms are ordered by id I guess, in the same order as in the backend.我猜这些术语按 id 排序,与后端的顺序相同。

I had this problem before, but then I added 'menu_order' => false and it worked.我以前遇到过这个问题,但后来我添加了'menu_order' => false并且它起作用了。 But since the update nothing works any more.但自从更新后,什么都不起作用了。

$terms = get_terms( 'product_cat', array(
       'orderby'           => 'name', 
       'order'             => 'ASC',
       'parent' => 2063,
       'menu_order' => false,
       'suppress_filter' => false
   ) );

I Also tried to switch themes without success.我也尝试切换主题没有成功。 Any help is welcome.欢迎任何帮助。

I use WordPress version 5.1.1 and Storefront theme version 2.4.5.我使用 WordPress 5.1.1 版和 Storefront 主题 2.4.5 版。

Update 2 - Solved更新 2 - 已解决

After I reported this issue on Github WooCommerce , it was clearly a bug affecting orderby argument when calling get_terms() function.在我在 Github WooCommerce 上报告了这个问题后,这显然是调用get_terms()函数时影响orderby参数的错误。 It's now approved and patched .现在已经批准并修补了

The issue is solved on Woocommerce update 3.6.2该问题已在 Woocommerce 更新 3.6.2 上解决


First since WordPress 4.5 taxonomies should be passed via the 'taxonomy' argument in the argument array on get_terms() function.首先,因为 WordPress 4.5 分类法应该通过get_terms()函数的参数数组中的“分类法”参数传递。

You can not use 'menu_order' and 'suppress_filter' arguments, as they are not defined for WP_Term_Query Class.您不能使用'menu_order''suppress_filter'参数,因为它们不是WP_Term_Query定义的。 Instead of 'menu_order' , you will use 'orderby' => 'order', .您将使用'orderby' => 'order',代替'menu_order'


Now the allowed arguments for WordPress get_terms() function are listed in WP_Term_Query __construct() .现在WP_Term_Query __construct()中列出了 WordPress get_terms()函数允许的参数

So YES you can use **" orderby " argument:**所以是的,你可以使用 **" orderby "参数:**

  • The defaults value is set to name .默认值设置为name
  • Accepts term fields ( name , slug , term_group , term_id , id , description , parent ), count for term taxonomy count, include to match the order of the $include param, slug__in to match the order of the $slug param, meta_value , meta_value_num , the value of $meta_key , the array keys of $meta_query, or none to omit the ORDER BY clause.接受词字段( nameslugterm_groupterm_ididdescriptionparent ), count的术语分类计数, include匹配order的$包括PARAM的, slug__in匹配order的$塞PARAM的, meta_valuemeta_value_num , $meta_key 的值, $meta_query 的数组键,或none以省略 ORDER BY 子句。

So your code should be something like:所以你的代码应该是这样的:

$terms = get_terms( 'product_cat', array(
    'taxonomy' => 'product_cat',
    // 'orderby'  => 'name', // <=== Default orderby is already 'name'
    'order'    => 'ASC',
    'parent'   => 2063,
) );

Tested and测试和

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM