WP_Query 和 acf 日期字段排序不正确

发布于 2025-01-17 09:11:13 字数 524 浏览 2 评论 0原文

我试图只看到与今天之后或相等的帖子,然后对ASC进行排序,我的日期字段输出YMD,第一部分(仅在当今或等于今天的帖子)工作,但排序却没有。

$today = date ('Ymd');
$args = array(
    'post_type' => 'diensten', 
    'posts_per_page'    => -1,   
    'meta_query' => array(
        array(
            'key' => 'datum_dienst',
            'value' => $today,
            'compare' => '>='
        )
    ),
    'meta_key'  => 'datum_dienst',
    'orderby'   => 'meta_value_num',
    'order'     => 'ASC',
);
$loop = new WP_Query( $args );

I am trying to only see posts after or equal to today and sort ASC, my date field outputs Ymd, the first part (only see posts after or equal to today) works but the sorting does not.

$today = date ('Ymd');
$args = array(
    'post_type' => 'diensten', 
    'posts_per_page'    => -1,   
    'meta_query' => array(
        array(
            'key' => 'datum_dienst',
            'value' => $today,
            'compare' => '>='
        )
    ),
    'meta_key'  => 'datum_dienst',
    'orderby'   => 'meta_value_num',
    'order'     => 'ASC',
);
$loop = new WP_Query( $args );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夕嗳→ 2025-01-24 09:11:13

对于面临同样问题的少数人,这是我非常具体的解决方案。

如果您安装了 WordPress 插件 Post Types Order,它将覆盖您的订单查询,我卸载了它,一切都神奇地工作了!

For the few people facing the same problem, here is my very specific solution.

If you have the wordpress plugin Post Types Order installed it will override your order query, i uninstalled it and everything magically worked!

与君绝 2025-01-24 09:11:13

如果您在按日期排序数据时遇到问题,可以添加 'meta_type' =>; 'DATE' 设置元类型。

如果您不使用 meta_query 而只是想对数据进行排序,也可以解决该问题。

所以它将是:

'meta_key'  => 'datum_dienst',
'orderby'   => 'meta_value',
'order'     => 'ASC',
'meta_type' => 'DATE'

更多信息:https://developer.wordpress.org/reference/classes /wp_query/

使用“meta_value_num”代替数值。您还可以指定
如果要将元值转换为特定类型,请使用“meta_type”。
可能的值为“NUMERIC”、“BINARY”、“CHAR”、“DATE”、“DATETIME”、
'DECIMAL'、'SIGNED'、'TIME'、'UNSIGNED',与 '$meta_query' 中的相同。

If you are having trouble with data getting sort by dates, you can add 'meta_type' => 'DATE' to set the meta type.

Also solves the issue if you are not using a meta_query but just want to sort the data.

So it will be:

'meta_key'  => 'datum_dienst',
'orderby'   => 'meta_value',
'order'     => 'ASC',
'meta_type' => 'DATE'

More info: https://developer.wordpress.org/reference/classes/wp_query/

Use ‘meta_value_num‘ instead for numeric values. You may also specify
‘meta_type‘ if you want to cast the meta value as a specific type.
Possible values are ‘NUMERIC’, ‘BINARY’, ‘CHAR’, ‘DATE’, ‘DATETIME’,
‘DECIMAL’, ‘SIGNED’, ‘TIME’, ‘UNSIGNED’, same as in ‘$meta_query‘.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文