开发者

wordpress query posts, get all posts for current month from custom field date

开发者 https://www.devze.com 2023-02-13 00:40 出处:网络
I have an events custom post type in wp 3.1I am using the following query_posts: <?php query_posts(\'post_type=event&me开发者_运维知识库ta_key=event_date&orderby=meta_value&order=ASC\'

I have an events custom post type in wp 3.1 I am using the following query_posts:

<?php query_posts('post_type=event&me开发者_运维知识库ta_key=event_date&orderby=meta_value&order=ASC'); ?>

As you can see I am ordering by the custom post type date. My issue is that I want to list only the events for the current month. Query posts offers monthnum but then I need to pass the monthnum into the query and I have no idea how that would even be possible. The date format is YYYY-mm-dd


If we were to get the date of the publication of the post it would be something like:

$today = getdate();
query_posts( 'year=' . $today["year"] . '&monthnum=' . $today["mon"] . '&post_type=event&meta_key=event_date&orderby=meta_value&order=ASC' );

But to get the date from the meta values, have you tried?:

query_posts( array( 'meta_key' => 'event_date', 'meta_value' => '2011-02-01', 'meta_compare' => '>=', 'post_type' => 'event' ) );

You can get the first day of the current month with something like:

date( "m/d/Y", strtotime(date('m').'/01/'.date('Y') );

Otherwise, I would try by retrieving all the posts as you were doing and then make date objects with each meta value and compare its month and year with current.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号