网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

Wordpress - 按meta_value_num排序无效

GG网络技术分享 2025-03-18 16:12 3


问题描述:

I have a meta on my taxonomies terms that I increment each time they are used, like this :

$count = get_term_meta($id, \'used\', true);

update_term_meta($id, \'used\', $count ? (intval($count) + 1) : 1);

But when I\'m trying to get terms ordered by this meta, the meta_value_num doesn\'t seem to do anything and my terms are still ordered like if the used meta was a string.

$terms = get_terms([

\'taxonomy\' => \'contract_type\',

\'meta_key\' => \'used\',

\'order_by\' => \'meta_value_num\',

]);

foreach ($types as $key => $type) {

$types[$key]->used_count = get_term_meta($type->term_id, \'used\', true);

$types[$key]->used_count_type = gettype($types[$key]->used_count);

}

And this is what is returned :

Array

(

[0] => WP_Term Object

(

...

[used_count] => 110

[used_count_type] => string

)

[1] => WP_Term Object

(

...

[used_count] => 1995

[used_count_type] => string

)

[2] => WP_Term Object

(

...

[used_count] => 810

[used_count_type] => string

)

)

I\'ve tried every variation of the request as I could find (with meta_query instead, meta_type, etc) without luck, so I\'m suspecting that the problem is coming from the type of the meta, but I don\'t see what else I could do to force him to save an int instead of a string.

Any ideas ?

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

我的分类学术语中有一个meta,我每次使用时都会增加,如:</ p>

  $ count = get_term_meta($ id,\'used\',true); 

update_term_meta($ id,\'used\',$ count?(intval($ count)+ 1):1) ;

</ code> </ pre>

但是当我试图通过这个元素排序时, meta_value_num </ code>似乎没有做任何事情而且我的 如果使用的</ em>元数据是一个字符串,那么这些术语仍然是有序的。</ p>

  $ terms = get_terms([

\'taxonomy\'=&gt;\'contract_type \',

\'meta_key\'=&gt;\'used\',

\'order_by\'=&gt;\'meta_value_num\',

]);

foreach($ types as $ key =&gt; $ type){

$ types [$ key] - &gt; used_count = get_term_meta($ type-&gt; term_id,\'used\',true);

$ types [$ key] - &gt; used_count_type = gettype($ types [$ key] - &gt; used_count );

}

</ code> </ pre>

这是返回的内容:</ p>

  Array 

[ 0] =&gt; WP_Term对象

...

[used_count] =&GT; 110

[used_count_type] =&gt; string

[1] =&gt; WP_Term对象

...

[used_count] =&gt; 1995

[used_count_type] =&gt; string

[2] =&gt; WP_Term对象

...

[used_count] =&gt; 810

[used_count_type] =&gt; string

</ code> </ pre>

我已经尝试了所有请求的变体(我可以找到 meta_query </ code>, meta_type </ code>等)而没有运气,所以我怀疑这个问题 是来自meta的类型,但是我没有看到我还能做些什么来强迫他保存 int </ code>而不是 string </ code>。</ p>

任何想法?</ p>

</ div>

网友观点:

I think it\'s orderby instead of order_by, isn\'t it?

标签:

提交需求或反馈

Demand feedback