drgc9632 2016-03-05 11:50
浏览 61
已采纳

使用meta_query进行确切的日期范围搜索

I have a date range search tool on a wordpress website to help find events that happen within a particular time-frame. I am using ACF pro's repeater to list dates within an event post. The date range tool is correctly pulling up events where dates for the event occurs within the date range, but it is also including events which do not have dates listed within the date range but where the first and last date are either side of the the date range.

I need to find a solution that means only events show up in the date range search if they have an exacting date within range.

Here is the loop / meta_query I'm using:

<?php  // filter
function my_posts_where( $where ) {

$where = str_replace("meta_key = 'showings_%", "meta_key LIKE 'showings_%", $where);

return $where;
}

add_filter('posts_where', 'my_posts_where');

  $_sda = $_GET['sda'] != '' ? $_GET['sda'] : '';
  $_smo = $_GET['smo'] != '' ? $_GET['smo'] : '';
  $_syr = $_GET['syr'] != '' ? $_GET['syr'] : '';

  $startd = $_syr.$_smo.$_sda.'0000';

  $_eda = $_GET['eda'] != '' ? $_GET['eda'] : '';
  $_emo = $_GET['emo'] != '' ? $_GET['emo'] : '';
  $_eyr = $_GET['eyr'] != '' ? $_GET['eyr'] : '';

    $endd = $_eyr.$_emo.$_eda.'2359';

    $meta_query = array(
                        'posts_per_page'    => -1,
                        'post_type'     => 'events',
                        'meta_key'      => 'showings_%_show_when',
                        'orderby'   => 'meta_value',
                        'order' => 'ASC',
                        'meta_query' => array(
                            'relation' => 'BETWEEN',
                            array(
                                'key'       => 'showings_%_show_when',
                                'value'     => $startd,
                                'compare'   => '>=',
                                'type'      => 'NUMERIC'
                            ),
                            array(
                                'key'       => 'showings_%_show_when',
                                'value'     => $endd,
                                'compare'   => '<=',
                                'type'      => 'NUMERIC'
                            )
                        )
                    );

    // query
    $the_query = new WP_Query( $meta_query ); ?>

I hope this is enough information to go on. Here's a live example if it helps.

The events that are appearing incorrectly are LegaC and MyMoves - the events themselves do not occur during this time period.

Thanks very much,

  • 写回答

1条回答 默认 最新

  • douyinghuo8874 2016-03-09 16:59
    关注

    This has been resolved with a bit of redrafting of the query. See here:

    $meta_query = array(
                            'posts_per_page'    => -1,
                            'post_type'     => 'events',
                            'orderby'   => 'meta_value',
                            'order' => 'ASC',
                            'meta_query' => array(
                                'relation' => 'AND',
                                array(
                                    'key' => 'showings_%_show_when',
                                    'value' => array( $startd, $endd ),
                                    'type' => 'NUMERICAL',
                                    'compare' => 'BETWEEN'
                                )
                            )
                        );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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