按自定义字段日期列出自定义帖子类型 [英] List custom post type by custom field date

查看:187
本文介绍了按自定义字段日期列出自定义帖子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Wordpress和WP-Types插件,需要按照自定义字段日期对CPT进行排序。

I'm working with Wordpress and WP-Types plugin and need to sort CPT by a custom field date.

这个工作正常:

$args = array(
    'post_type' => 'parties',
    'paged' => $paged,
    'meta_key' => 'wpcf-parties_date',
    'orderby' => 'meta_value',
    'order' => 'DESC',
);

orderby工作完美。
在数据库中,字段date'wpcf-parties_date'的值以这种方式存储为:1349481600。

The orderby works perfect. On the Database the value of the field date 'wpcf-parties_date' is store in this way ex: 1349481600 .

我有两个模板,我需要显示过去和即将到来的帖子。我的问题是如何才能显示有关当前日期的未来或过去的帖子?

I have two templates where i need to show past and upcoming Posts. My question is how can i display only the future or past posts regarding the current date?

我没有运气尝试这个。

$args = array(
    'post_type' => 'parties',
    'paged' => $paged,
    'meta_key' => 'wpcf-parties_date',
    'meta_value' => date('d.m.Y H:i:s'),
    'meta_compare' => '>',
    'orderby' => 'meta_value',
    'order' => 'ASC'
);

我使用WP_Query进行循环。

I using WP_Query for the loop.

提前感谢!

推荐答案

您可以尝试 meta_query 来检查 <和> ,并且还使用 meta_value_num ,因为您的自定义字段值是数字(1349481600)。

You may try meta_query to check both < and > and also use meta_value_num because your custom field value is numeric (1349481600).

$current_date=strtotime(date('d.m.Y H:i:s'));
$args = array(
    'post_type' => 'parties',
    'paged' => $paged,
    'meta_key' => 'wpcf-parties_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'meta_query' => array(
        array(
            'key' => 'wpcf-parties_date',
            'value' => $current_date,
            'compare' => '>'
        ),
        array(
            'key' => 'wpcf-parties_date',
            'value' => $current_date,
            'compare' => '<'
        )
    )
);

阅读文档了解更多

这篇关于按自定义字段日期列出自定义帖子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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