自定义wordpress元查询(无结果)

时间:2022-03-31 16:38:26

Problem

I am looping through custom post types (Advanced Custom Fields) in Wordpress. I only want to display events with start_date equal to $newdate variable, defined in the beginning.

我正在循环浏览Wordpress中的自定义帖子类型(高级自定义字段)。我只想显示start_date等于$ newdate变量的事件,该变量在开头定义。

The start_date is in the format YYYY-MM-DD HH:mm (the same as $newdate). $newdate is set to the beginning of the day so I wouldn't exclude events with different hours in the day and compare is set to greater than (just to test the query).

start_date的格式为YYYY-MM-DD HH:mm(与$ newdate相同)。 $ newdate设置为当天的开头,因此我不会排除当天不同小时的事件,并且compare设置为大于(仅用于测试查询)。

However I am not getting any results.

但是我没有得到任何结果。

<?php
$newdate = date('Y-m-d 00:00');
//<-- Start the Loop. -->!
$args = array( 
        'post_type' => 'epsa_events', 
        'posts_per_page' => 5,
        'orderby' => 'meta_value',
        'order' => 'ASC',
        'meta_query' => array (
            array(
            'key' => 'start_time',
            'value'   => $newdate,
            'compare' => '>=',
            'type' => 'datetime' 
            )
        )
    );

$loop = new WP_Query( $args );

2 个解决方案

#1


0  

Try this query:-

试试这个查询: -

'meta_key'   =>  'event-start-date',
'meta_query' => array (
            array(
            'key' => 'start_time',
            'value' => date('Ymd',strtotime($newdate)),
            'compare' => '>=',
            'type' => 'date' 
            )
        )

#2


0  

I guess you have some small mistakes here.

我猜你这里有一些小错误。

First, if you use 'orderby' => 'meta_value' you must add a 'meta_key' => KEYVALUE to your $args where KEYVALUE is the name of the custom field you want to use for sorting. Check WP_Query documentation on Order & Orderby Parameters.

首先,如果使用'orderby'=>'meta_value',则必须在$ args中添加'meta_key'=> KEYVALUE,其中KEYVALUE是您要用于排序的自定义字段的名称。检查Order&Orderby Parameters上的WP_Query文档。

Second, assuming your start_date field type is Date Time Picker, if you want to get all events that already started you're using the wrong comparison operator. So assuming you also want to sort by date, your $args code should be:

其次,假设您的start_date字段类型是Date Time Picker,如果您想要获取已经启动的所有事件,那么您使用的是错误的比较运算符。因此,假设您还想按日期排序,您的$ args代码应为:

$args = array(
    'post_type'      => 'epsa_events',
    'posts_per_page' => -1,
    'orderby'        => 'meta_value',
    'order'          => 'ASC',
    'meta_key'       => 'start_time',
    'meta_type'      => 'DATETIME'
    'meta_query'     => array (
        array(
            'key'     => 'start_time',
            'compare' => '<=',
            'value'   => $newdate,
            'type'    => 'DATETIME'
        )
    )
);

Take a look at Date Time Picker documentation too.

请查看日期时间选择器文档。

I hope this helps!

我希望这有帮助!

#1


0  

Try this query:-

试试这个查询: -

'meta_key'   =>  'event-start-date',
'meta_query' => array (
            array(
            'key' => 'start_time',
            'value' => date('Ymd',strtotime($newdate)),
            'compare' => '>=',
            'type' => 'date' 
            )
        )

#2


0  

I guess you have some small mistakes here.

我猜你这里有一些小错误。

First, if you use 'orderby' => 'meta_value' you must add a 'meta_key' => KEYVALUE to your $args where KEYVALUE is the name of the custom field you want to use for sorting. Check WP_Query documentation on Order & Orderby Parameters.

首先,如果使用'orderby'=>'meta_value',则必须在$ args中添加'meta_key'=> KEYVALUE,其中KEYVALUE是您要用于排序的自定义字段的名称。检查Order&Orderby Parameters上的WP_Query文档。

Second, assuming your start_date field type is Date Time Picker, if you want to get all events that already started you're using the wrong comparison operator. So assuming you also want to sort by date, your $args code should be:

其次,假设您的start_date字段类型是Date Time Picker,如果您想要获取已经启动的所有事件,那么您使用的是错误的比较运算符。因此,假设您还想按日期排序,您的$ args代码应为:

$args = array(
    'post_type'      => 'epsa_events',
    'posts_per_page' => -1,
    'orderby'        => 'meta_value',
    'order'          => 'ASC',
    'meta_key'       => 'start_time',
    'meta_type'      => 'DATETIME'
    'meta_query'     => array (
        array(
            'key'     => 'start_time',
            'compare' => '<=',
            'value'   => $newdate,
            'type'    => 'DATETIME'
        )
    )
);

Take a look at Date Time Picker documentation too.

请查看日期时间选择器文档。

I hope this helps!

我希望这有帮助!