查询 post_meta 日期而不是 post_date_gmt
Query post_meta date instead of post_date_gmt
我将查询限制为使用 6 个月前的 show post,效果很好。
但我需要它基于 post_meta
table 中的日期而不是 'post_date_gmt'。
在我的例子中,我有 meta_keys 被称为 payment_date
并且值当然是像 [=15= 这样的日期] 例如。
$months_ago = 6;
$args = array(
'date_query' => array(
array(
'column' => 'post_date_gmt',
'after' => $months_ago . ' months ago',
)
),
'numberposts' => -1
);
在这里查看:https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
如果滚动示例,您会看到:
Display posts where the custom field key is a set date and the custom field value is now. Displays only posts which date has not passed.
$args = array(
'post_type' => 'event',
'meta_key' => 'event_date',
'meta_value' => date( "Ymd" ), // change to how "event date" is stored
'meta_compare' => '>',
);
$query = new WP_Query( $args );
在你的情况下,你可以这样写:date( "dmY", strtotime( '6 months ago' ) )
First thing first date_query
works on post_date_gmt
. If you want
to query post from meta fields then you have to use meta_query
.
这是一个示例代码:
$months_ago = 6;
$args = [
//...
//...
'posts_per_page' => -1, // Unlimited posts
//...
//...
'meta_query' => [
'relation' => 'AND',
[
'key' => 'payment_date',
'value' => date('d-m-Y', strtotime($months_ago . ' months ago')), //<-- Converting date into your custom date format
'compare' => '>', //you can also use <=, >=, <, etc..
'type' => 'DATE'
],
]
];
$query = new WP_Query($args);
if ($query->have_posts()) :
/* Start the Loop */
while ($query->have_posts()) : $query->the_post();
//you post
endwhile;
endif;
上面的代码应该适合你。
相关问题:
- Hide past events posts based on custom date field
希望对您有所帮助!
试试这个代码:
$sixmonthagodate = date( "d-m-Y", strtotime('-6 Months') );
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_key' => 'payment_date',
'meta_value' => $sixmonthagodate,
'meta_compare' => '>',
);
$query = new WP_Query( $args );
检查您的 post_type 在您的 WordPress 站点中是否正确。
我将查询限制为使用 6 个月前的 show post,效果很好。
但我需要它基于 post_meta
table 中的日期而不是 'post_date_gmt'。
在我的例子中,我有 meta_keys 被称为 payment_date
并且值当然是像 [=15= 这样的日期] 例如。
$months_ago = 6;
$args = array(
'date_query' => array(
array(
'column' => 'post_date_gmt',
'after' => $months_ago . ' months ago',
)
),
'numberposts' => -1
);
在这里查看:https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
如果滚动示例,您会看到:
Display posts where the custom field key is a set date and the custom field value is now. Displays only posts which date has not passed.
$args = array(
'post_type' => 'event',
'meta_key' => 'event_date',
'meta_value' => date( "Ymd" ), // change to how "event date" is stored
'meta_compare' => '>',
);
$query = new WP_Query( $args );
在你的情况下,你可以这样写:date( "dmY", strtotime( '6 months ago' ) )
First thing first
date_query
works onpost_date_gmt
. If you want to query post from meta fields then you have to usemeta_query
.
这是一个示例代码:
$months_ago = 6;
$args = [
//...
//...
'posts_per_page' => -1, // Unlimited posts
//...
//...
'meta_query' => [
'relation' => 'AND',
[
'key' => 'payment_date',
'value' => date('d-m-Y', strtotime($months_ago . ' months ago')), //<-- Converting date into your custom date format
'compare' => '>', //you can also use <=, >=, <, etc..
'type' => 'DATE'
],
]
];
$query = new WP_Query($args);
if ($query->have_posts()) :
/* Start the Loop */
while ($query->have_posts()) : $query->the_post();
//you post
endwhile;
endif;
上面的代码应该适合你。
相关问题:
- Hide past events posts based on custom date field
希望对您有所帮助!
试试这个代码:
$sixmonthagodate = date( "d-m-Y", strtotime('-6 Months') );
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_key' => 'payment_date',
'meta_value' => $sixmonthagodate,
'meta_compare' => '>',
);
$query = new WP_Query( $args );
检查您的 post_type 在您的 WordPress 站点中是否正确。