wordpress 帖子查询包括元和大于日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16596551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:14:22  来源:igfitidea点击:

Posts query including meta and greater than date

wordpressdate

提问by Paul

I'm struggling to get a working solution with this wp_query. I currently have some custom settings which are assigned to posts, one is whether or not the post is 'featured' and the second is a date and time for the post to end (no longer display in the results). I have the query working with the feature, but just need to work this end date into it, here is the query working find with the 'featured':

我正在努力为此找到一个可行的解决方案wp_query。我目前有一些分配给帖子的自定义设置,一个是帖子是否“精选”,第二个是帖子结束的日期和时间(不再显示在结果中)。我有使用该功能的查询,但只需要将此结束日期输入其中,这是使用“特色”查找的查询:

WP_Query('meta_key=skyali_feature&showposts=4&orderby=post_date');

The end date is set in the wp_postmetatable where meta_keyis 'the_date' and the meta_valueslook like this '05/16/2013 05:24'. I would like to edit the above query where if 'the_date' has been set posts are only included if the 'the_date' is greater that todays date and time.

结束日期在wp_postmeta表中设置,其中meta_key'the_date'meta_values看起来像这样 '05/16/2013 05:24'。我想编辑上面的查询,如果 'the_date' 已设置,则仅当 'the_date' 大于今天的日期和时间时才包含帖子。

Here is my failed attempt:

这是我失败的尝试:

WP_Query(
   'meta_key=skyali_feature&meta_key=the_date&meta_compare=>=&meta_value='
   .date('d/m/Y H:i')
   .'&showposts=4&orderby=post_date&orderby=the_date'
);

回答by Jared Cobb

I had to do something very similar recently and ended up needing to use the meta_queryproperty instead. You'll want to do something like this:

我最近不得不做一些非常相似的事情,最终需要使用该meta_query属性。你会想要做这样的事情:

$today = date('Ymd');
$args = array(
    'post_type' => 'post',
    'posts_per_page' => '4',
    'meta_key' => 'the_date',
    'meta_query' => array(
        array(
            'key' => 'skyali_feature'
        ),
        array(
            'key' => 'the_date',
            'value' => $today,
            'compare' => '>='
        )
    ),
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
);

$your_custom_query = new WP_Query($args);

A few notes...

一些注意事项...

  • I only needed to filter by date in my example, but it looks like you'll need to do date/time in yours. (You can just adjust the first line for the $todayvariable using the format you wish).

  • Use posts_per_pageinstead of showposts. showpostsis deprecated.

  • Notice that I have included the meta_keytwice (once at the top level of the array and once as an element in the meta_queryarray. There's a known bug where you can't sort your results by the key if you don't include it this way. I fought that one for a while too!

  • 我只需要在我的示例中按日期过滤,但看起来您需要在您的日期/时间中进行。(您可以$today使用您希望的格式调整变量的第一行)。

  • 使用posts_per_page代替showpostsshowposts已弃用。

  • 请注意,我已经包含了meta_key两次(一次在数组的顶层,一次作为数组中的元素meta_query。有一个已知的错误,如果您不以这种方式包含它,则您无法按键对结果进行排序。我也和那个人战斗了一段时间!

Hope this helps, have fun!

希望这会有所帮助,玩得开心!

[edit]Forgot to add your skyali_featurekey back into the array.

[编辑]忘记将您的skyali_feature密钥添加回数组。

回答by Sebastian Viereck

for people using the advanced custom field plugin with a date data type, this is what you need for dates greater or equal than today:

对于使用具有日期数据类型的高级自定义字段插件的人,这就是大于或等于今天的日期所需要的:

    $today = date('Ymd');
    $args = array(
        'post_type' => 'post',            
        'meta_key' => 'end-date',
        'meta_query' => array(
            array(
                'key' => 'end-date'
            ),
            array(
                'key' => 'end-date',
                'value' => $today,
                'compare' => '>='
            )
        ),
        'orderby' => 'meta_value',
        'order' => 'ASC'
    );
   $your_custom_query = new WP_Query($args);

回答by balubino

for people using Custom metadata manageryou'll find that a datepickerfield is stored as timestamp.

对于使用自定义元数据管理器的人,您会发现一个datepicker字段存储为时间戳。

So in a similar case the above example isn't working and you may have php sort out the value you need to compare against. And the timestamp for a day earlier at 23:59:59 it'll do the job:

所以在类似的情况下,上面的例子不起作用,你可能让 php 整理出你需要比较的值。以及前一天 23:59:59 的时间戳,它将完成这项工作:

    $yesterday = strtotime('yesterday 23:59:59');
    $args = array(
        'post_type' => 'post',            
        'meta_key' => 'end-date',
        'meta_query' => array(
            array(
                'key' => 'end-date'
            ),
            array(
                'key' => 'end-date',
                'value' => $yesterday,
                'compare' => '>='
           )
        ),
        'orderby' => 'meta_value',
        'order' => 'ASC'
    );
    $your_custom_query = new WP_Query($args);

If you also want to take account of the timezone setting for the blog use current_time()as in the following example:

如果您还想考虑博客的时区设置,请使用current_time(),如下例所示:

    $now = current_time('timestamp');
    $yesterday = mktime(23, 59, 59, date('n',$now), date('j',$now)-1);