dsfdsfdsfdsf1223 2015-04-30 22:19
浏览 5

按多个元值排序Wordpress帖子

Using get_posts(), I need to first retrieve posts that fall on a certain day (the day is set by a custom field - just the date, not time). I do this by using a meta key/value. Then, I need to order these posts based on the time of day (which is a separate custom field, just time, not date). So essentially I need to pull in all the events that fall on a given day, and order them according to the time.

First I grab the day, using a custom field:

if ( get_field('festival_day') ) {
  $day_stamp = get_field('festival_day');
}

Then I set my arguments for the query:

$args = array(
        'posts_per_page' => -1,
        'post_type' => 'event',
        'meta_key' => 'event_date',
        'meta_value' => $day_stamp
    );
    $events = get_posts( $args );

So.. the question is, how do I query the other custom field (which is the start time), and then sort by that time? The time field key is event_start_time.

Thanks!

  • 写回答

2条回答 默认 最新

  • dongshi1424 2015-05-01 04:59
    关注

    You can user WP_Query to retrieve your events and you can query it something like below:

    $args = array(
        'post_type' => 'event',
        'order' => 'DESC',
        'orderby' => 'meta_value',
        'meta_key' => 'event_start_time',
        'meta_query' => array(
            array(
                'key' => 'event_start_time',
                'value' => 'yourValue_here',
                'compare' => '>='
            ),
            array(
                'key' => 'event_date',
                'value' => $day_stamp,
                'compare' => '='
            )
        )
    );
    $query = new WP_Query( $args );
    

    UNTESTED but it should work.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看