DATE_QUERY不接受DAY参数,但将接受自定义帖子的月和年

时间:2016-07-04 作者:C.MO

我有一个令人沮丧的问题。我正在使用date_query 如法典所述:

<?php
$today = getdate();
$args = array(
\'post_type\'         => \'Lighting\',
\'post_status\'       => \'publish\',
\'posts_per_page\'    => 1,
\'date_query\' => array(
        array(
            \'year\'  => $today[\'year\'],
            \'month\' => $today[\'mon\'],
            \'day\'   => $today[\'mday\'],
        ),
    ),
);
$the_query = new WP_Query( $args );
如果排除,则可以按预期显示自定义帖子\'day\', 当它包含在查询中时,我没有收到任何帖子。我正在使用高级自定义字段Prodate-picker. 我不知道为什么会发生这种情况,我一直在不知疲倦地寻找date_query 不工作。我能回显日期,所以我不知道断开的地方在哪里。

/******对答案的回应******/

感谢您的回复。我已经用我认为合适的日期格式进行了meta\\u查询,但我仍然无法仅查询今天的帖子。以下是新查询:

<?php // Let\'s get the data we need to loop through below

$args = array( 
        \'post_type\' => \'carillon\', // Tell WordPress which post type we want
        \'orderby\' => \'meta_value\', // We want to organize the events by date    
        \'meta_key\' => \'date_of_lighting\', // Grab the "date_of_event" field created via "date-picker" plugin (stored in ‘YYYYMMDD’)
        \'posts_per_page\' => \'1\', // Let\'s show just one post.   
        \'meta_query\' => array( // WordPress has all the results, now, return only the event on today\'s date
            array(
                \'key\' => \'date_of_lighting\', // Check the s"date_of_lighting field
                \'value\' => date("Y-M-D"), // Set today\'s date (note the similar format)
                \'compare\' => \'=\', // Return only today\'s post
                \'type\' => \'NUMERIC\' // Let WordPress know we\'re working with numbers
                )
            )
    );
有什么建议吗?再次感谢。

/********解决方案**********/

大家好,

所以,我找到了一个可行的解决方案。我将类型更改为“日期”。有趣的是,在其他示例中,人们希望显示今天及以后的日期,他们使用“数字”类型。我想这是有道理的,但我将跳进法典来进一步理解这一点。我感谢你的帮助。以下是有效的解决方案:

                        <?php // Let\'s get the data we need to loop through below
                            $args = array( 
                                    \'post_type\' => \'carillon\', // Tell WordPress which post type we want
                                    \'orderby\' => \'meta_value\', // We want to organize the events by date    
                                    \'meta_key\' => \'date_of_lighting\', // Grab the "date_of_event" field created via "date-picker" plugin (stored in ‘YYYYMMDD’)
                                    \'posts_per_page\' => \'1\', // Let\'s show just one post.   
                                    \'meta_query\' => array( // WordPress has all the results, now, return only the event for today\'s date
                                        array(
                                            \'key\' => \'date_of_lighting\', // Check the s"date_of_lighting field
                                            \'value\' => date("Y-m-d"), // Set today\'s date (note the similar format)
                                            \'compare\' => \'=\', // Return only today\'s post
                                            \'type\' => \'DATE\' // Let WordPress know we\'re working with a date
                                            )
                                        )
                                );

2 个回复
SO网友:user1049961

正如评论中所说,你应该使用meta_query.

有关如何按ACF元字段查询帖子的帮助,您可以查看ACF文档:

https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

$args = array(
    \'numberposts\'   => -1,
    \'post_type\'     => \'event\',
    \'meta_key\'      => \'location\',
    \'meta_value\'    => \'Melbourne\'
);


// query
$the_query = new WP_Query( $args );

SO网友:C.MO

这里是解决方案。我对每一行都发表了评论,以澄清发生了什么。希望这一切都是有道理的,而且是正确的:)

我感谢rockstar社区对我的帮助。

        <?php // Let\'s get the data we need to loop through below
    $args = array( 
            \'post_type\' => \'carillon\', // Tell WordPress which post type we want
            \'orderby\' => \'meta_value\', // We want to organize the events by date    
            \'meta_key\' => \'date_of_lighting\', // Grab the "date_of_event" field created via "date-picker" plugin (stored in ‘YYYYMMDD’)
            \'posts_per_page\' => \'1\', // Let\'s show just one post.   
            \'meta_query\' => array( // WordPress has all the results, now, return only the event for today\'s date
                array(
                    \'key\' => \'date_of_lighting\', // Check the "date_of_lighting" field
                    \'value\' => date("Y-m-d"), // Set today\'s date (note the similar format)
                    \'compare\' => \'=\', // Return only today\'s post
                    \'type\' => \'DATE\' // Let WordPress know we\'re working with numbers
                      )
                    )
                  );

                        $the_query = new WP_Query($args); ?>

                            <?php if ( $the_query->have_posts() ) : ?>                                  

                                    <!-- the loop-->