drvpv7995 2017-11-01 20:28 采纳率: 100%
浏览 44

按ACF日期选择器排序查询自定义帖子无法正常工作?

I have a custom post type 'event' that I am trying to sort by date. And for some reason it isn't working. I'm confused because I've actually already done this before without a problem. What wrong this time around?

The first three lines are to get the month, year, and state that are assigned to the page via ACF custom fields. (This is separate from the ACF fields that the events have.) Essentially I'm trying to get the events only for that state, month, and year.

<?php 
            $thestate = get_field('page_state');
            $themonth = get_field('page_month');
            $theyear = get_field('page_year');

            $meta_query = array(
                array(
                    'key'       => 'event_state',
                    'value'     => $thestate,
                    'compare'   => '='
                ),
                array(
                    'key'       => 'event_month',
                    'value'     => $themonth,
                    'compare'   => '='
                )
            );

            $args = array(
                'meta_key'          => 'date_of_event',
                'order_by'          => 'meta_value_num',
                'order'             => 'ASC',
                'post_type'         => 'event',
                'posts_per_page'    => -1,
                'meta_query'        => $meta_query
            );

            $myposts = get_posts($args);

            if($myposts) { ?>

            <?php foreach($myposts as $post) { ?>
                <?php setup_postdata( $post ); ?>

                /*--Code Here--*/


                <?php wp_reset_postdata(); ?>
            <?php } ?>
            <?php } ?>

EDIT: I figured out the issue. It took me way too long to get this figured out but it turns out that it was caused by the fact that I was using 'order_by' whereas I needed to be using 'orderby'... Removing the underscore is all that needed to be done. I am unsure as to why this is the case though. 'order_by' seems to work for others so I'm still a little bit confused but glad I managed to fix it.

  • 写回答

1条回答 默认 最新

  • dousui7410 2017-11-01 21:38
    关注

    Your order_by is using 'meta_value_num' but it should probably be using 'meta_value' as your date is a string. You have to check if your stored date is in the format 'Y-m-d' otherwise sorting will not work correctly.

    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源