dongrao9436 2017-11-29 21:26
浏览 93
已采纳

Wordpress - 按meta_value_num排序无效

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 ?

  • 写回答

1条回答 默认 最新

  • doujiao1949 2017-11-29 21:46
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表