I'm limiting my query to use show post from 6 months ago which works fine.
我限制我的查询使用6个月前的show post工作正常。
But I need it to be based on a date that is in the post_meta
table instead of 'post_date_gmt'.
但我需要它基于post_meta表中的日期而不是'post_date_gmt'。
In my case I have meta_keys are called payment_date
and the values are of course a date like 31-10-2016
for example.
在我的情况下,我将meta_keys称为payment_date,值当然是例如31-10-2016的日期。
$months_ago = 6;
$args = array(
'date_query' => array(
array(
'column' => 'post_date_gmt',
'after' => $months_ago . ' months ago',
)
),
'numberposts' => -1
);
3 个解决方案
#1
2
First thing first
date_query
works onpost_date_gmt
. If you want to query post from meta fields then you have to usemeta_query
.首先,date_query适用于post_date_gmt。如果要从元字段查询帖子,则必须使用meta_query。
Here is a sample code:
这是一个示例代码:
$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;
The above code should work for you.
上面的代码应该适合你。
Related Question:
- Hide past events posts based on custom date field
- Orderby ACF custom field date don't work
根据自定义日期字段隐藏过去的事件帖子
订单ACF自定义字段日期不起作用
Hope this helps!
希望这可以帮助!
#2
3
Check it here: https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
请在此处查看:https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
If you scroll the examples, you'll see:
如果您滚动示例,您将看到:
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 );
In your case you could put something like: date( "dmY", strtotime( '6 months ago' ) )
在你的情况下,你可以把类似的东西:日期(“dmY”,strtotime('6个月前'))
#3
3
Try this code:
试试这段代码:
$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 );
check that if your post_type is true in your WordPress site.
检查您的WordPress网站中的post_type是否为true。
#1
2
First thing first
date_query
works onpost_date_gmt
. If you want to query post from meta fields then you have to usemeta_query
.首先,date_query适用于post_date_gmt。如果要从元字段查询帖子,则必须使用meta_query。
Here is a sample code:
这是一个示例代码:
$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;
The above code should work for you.
上面的代码应该适合你。
Related Question:
- Hide past events posts based on custom date field
- Orderby ACF custom field date don't work
根据自定义日期字段隐藏过去的事件帖子
订单ACF自定义字段日期不起作用
Hope this helps!
希望这可以帮助!
#2
3
Check it here: https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
请在此处查看:https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
If you scroll the examples, you'll see:
如果您滚动示例,您将看到:
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 );
In your case you could put something like: date( "dmY", strtotime( '6 months ago' ) )
在你的情况下,你可以把类似的东西:日期(“dmY”,strtotime('6个月前'))
#3
3
Try this code:
试试这段代码:
$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 );
check that if your post_type is true in your WordPress site.
检查您的WordPress网站中的post_type是否为true。