Wordpress使用日期元字段获取日期范围之间的帖子 [英] Wordpress get posts between date range using a date meta field

查看:45
本文介绍了Wordpress使用日期元字段获取日期范围之间的帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的帖子类型使用名为 start_date 的自定义元字段.我希望能够获得在date1和date2之间具有 start_date 的所有帖子.通过将查询类型声明为"DATE",我已经能够弄清楚如何使用wp_query在日期之前或之后获取帖子.

I have a post type that uses a custom meta field called start_date. I want to be able to get all posts that have a start_date between date1 and date2. I have been able to figure out how to get posts before or after a date using wp_query by declaring the query type as "DATE".

$queryargs = array('meta_key' => 'start_date', 'meta_value' => "2016-06-01", 'meta_compare' => '>', 'type' => 'DATE','posts_per_page' => $instance['pastlimit']);

这会在1月1日之后拉出带有start_date的帖子,但我也想使其限制下个月1号之后不会出现的日期.我不能再次添加相同的参数,因为它们未指定约束条件的差异.是否有可能更改查询,我必须在某个日期范围之间获取帖子.

This pulls posts with a start_date after the 1st, but i also want to make it limit dates that dont occur after the 1st of the next month. I cant add the same args again as they dont specify the difference in the constraint. Is it possible to alter the query i have to get posts between a date range.

推荐答案

您需要执行 meta_query 并比较 BETWEEN 一个由 DATE s组成的数组:

You need to do a meta_query and compare BETWEEN an array of DATEs:

$queryargs = array(
    'meta_query' => array(
        array(
            'key' => 'start_date', 
            'value' => array('2016-06-01', '2016-07-01'),
            'compare' => 'BETWEEN', 
            'type' => 'DATE',
        ),
    ),
    'posts_per_page' => $instance['pastlimit']
);

这篇关于Wordpress使用日期元字段获取日期范围之间的帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆