帖子查询包括meta和大于date [英] Posts query including meta and greater than date

查看:111
本文介绍了帖子查询包括meta和大于date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使用这个 wp_query 获得一个工作的解决方案。我目前有一些分配给帖子的自定义设置,一个是该帖子是否是特色,第二个是帖子结束的日期和时间(不再显示在结果中)。我有查询使用该功能,但只需要将其结束日期,这里是查询工作查找与'特色':

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('meta_key=skyali_feature&showposts=4&orderby=post_date');

结束日期设置在 wp_postmeta 表中 meta_key 是'the_date', meta_values 看起来像这样'05/16/2013 05:24'。我想编辑上面的查询,如果'the_date'已经设置了帖子,只有当'the_date'是更大的今天的日期和时间。

The end date is set in the wp_postmeta table where meta_key is 'the_date' and the meta_values look 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_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'
);


推荐答案

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

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

$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);

几个笔记...


  • 我只需要在我的例子中过滤日期,但是看起来你需要在你的日期/时间。 (您可以使用所需的格式调整 $ today 变量的第一行。

使用 posts_per_page 而不是 showposts

请注意,我已经将 meta_key 两次(一次在数组的顶层,一次作为 meta_query 数组中的元素,有一个已知错误,您无法排序结果如果你不这样做的话,我也打了一个钥匙。

Notice that I have included the meta_key twice (once at the top level of the array and once as an element in the meta_query array. 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!

希望这有助于,玩得开心!

Hope this helps, have fun!

忘记添加您的 skyali_feature 键返回数组。

这篇关于帖子查询包括meta和大于date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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