douxiza9868 2015-02-24 12:49
浏览 62

Wordpress - ACF - 获取重复开始和结束日期的帖子

Im running a travelling-site with journeys which have a start and ending date. Now these journeys can be several times a year.

Now I have a single-post with three repeated start and ending dates

01.03.2015 - 14.03.2015       2015-03-01  - 2015-03-14
01.04.2015 - 14.04.2015       2015-04-01  - 2015-04-14
01.05.2015 - 14.05.2015       2014-05-01  - 2015-05-14

I want to show my post with its start and ending dates contained in the search date range.

Searching 01.06.2015 - 01.07.2015 should return no results. Searching 01.03.2015 - 01.04.2015 should return one result.

$reisen_query_args = array(
'post_type' => 'reisen',
'post_status' => 'publish',
'meta_query' => array(
    array(
        'key'       => 'reisezeiten_%_start',
        'compare'   => '>=',
        'value'     => $startdate,
        'type'      => 'DATE'
    ),
     array(
        'key'       => 'reisezeiten_%_end',
        'compare'   => '<=',
        'value'     => $enddate,
        'type'      => 'DATE'
    )
)

This works half-way and I cant figure it out why:

if the search range is 01.03.2015 - 10.03.2015 (2015-03-01 - 2015-03-10) the post doesn't show up which is correct

BUT

if the range is 05.03.2015 - 16.03.2015 (2015-03-05 - 2015-03-16) the post shows up which is WRONG because the search starting date must be 01.03.2015 (2015-03.01) or before to return results.

AND

this only happens when Im having repeated fields. As long theres only one date stored 01.03.2015 - 14.03.2015 (2015-03-01 - 2015-03-14) the query behaves as desired.

Could someone pls help me with this? I'm searching for weeks now to get this done.

This is how my query looks like:

SELECT SQL_CALC_FOUND_ROWS wp410_posts.ID
FROM wp410_posts
INNER JOIN wp410_term_relationships
ON (wp410_posts.ID = wp410_term_relationships.object_id)
INNER JOIN wp410_postmeta
ON ( wp410_posts.ID = wp410_postmeta.post_id )
INNER JOIN wp410_postmeta AS mt1
ON ( wp410_posts.ID = mt1.post_id )
WHERE 1=1
AND ( wp410_term_relationships.term_taxonomy_id IN (29) )
AND wp410_posts.post_type = 'reisen'
AND ((wp410_posts.post_status = 'publish'))
AND ( ( wp410_postmeta.meta_key LIKE 'reisezeiten_%_start'
AND CAST(wp410_postmeta.meta_value AS DATE) >= '2015-03-02' )
AND ( mt1.meta_key LIKE 'reisezeiten_%_end'
AND CAST(mt1.meta_value AS DATE) <= '2015-03-14' ) )
GROUP BY wp410_posts.ID
ORDER BY wp410_posts.menu_order ASC
LIMIT 0, 6
  • 写回答

1条回答 默认 最新

  • dousi5358 2015-06-16 14:16
    关注

    The problem is that your request doesn't know that reisezeiten_1_start corresponds to reisezeiten_1_end. So it can uses reisezeiten_2_start and reisezeiten_1_start for your example.

    Your range : >=2015-03-05, <=2015-03-16

    Your data :

    2015-03-01  - 2015-03-14
    2015-04-01  - 2015-04-14
    2014-05-01  - 2015-05-14
    

    Your request means "I want post which have one start date greater than 2015-03-05 and one ending date less than 2015-03-16"

    So :

    2015-03-01  - 2015-03-14 => ending date correponds to your range
    2015-04-01  - 2015-04-14 => start date corresponds to your range
    2014-05-01  - 2015-05-14
    

    So you comparison is valid on the two meta_key and the post is returned. This explains why you code works with one date.

    Like said Matt. C, one solution would be to use multiple items in meta_query array (and like he said: it won't work if you have unlimited rows)

    $reisen_query_args = array(
        'post_type' => 'reisen',
        'post_status' => 'publish',
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'relation' => 'AND',
                array(
                    'key'       => 'reisezeiten_1_start',
                    'compare'   => '>=',
                    'value'     => $startdate,
                    'type'      => 'DATE'
                ),
                 array(
                    'key'       => 'reisezeiten_1_end',
                    'compare'   => '<=',
                    'value'     => $enddate,
                    'type'      => 'DATE'
                )
            ),
            array(
                'relation' => 'AND',
                array(
                    'key'       => 'reisezeiten_2_start',
                    'compare'   => '>=',
                    'value'     => $startdate,
                    'type'      => 'DATE'
                ),
                 array(
                    'key'       => 'reisezeiten_2_end',
                    'compare'   => '<=',
                    'value'     => $enddate,
                    'type'      => 'DATE'
                )
            ),
            array(
                'relation' => 'AND',
                array(
                    'key'       => 'reisezeiten_3_start',
                    'compare'   => '>=',
                    'value'     => $startdate,
                    'type'      => 'DATE'
                ),
                 array(
                    'key'       => 'reisezeiten_3_end',
                    'compare'   => '<=',
                    'value'     => $enddate,
                    'type'      => 'DATE'
                )
            )
    
        )
    )
    

    Be careful, this code works only on WP 4.1+

    See codex for more informations (end of paragraphe)

    Some help on ACF website

    (Sorry for my english)

    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?