Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a loop which displays all the posts of the "events" post type on my site. Each of these posts has a custom date field "event_date", which I sort the display of the loop by.

I would like to simplify my post management by only display posts which match the current month; that is, as of today (Dec.30) all the December posts display but on January 1, all the January posts display.

Note this is not post date, it's the value of the field (when the event takes place). I want to keep the month's worth of posts up ll month even when their date passes -- so always the complete month.

Most of the posts I found here with search are dealing with the post date, whereas I need to use the value of this custom field instead. Thanks. My current query:

$args = array( 'post_type' => 'hh_event', 'posts_per_page' => -1,'orderby' => 'meta_value', 'meta_key' => 'event_date', 'order' => 'ASC'  );
            $the_query = new WP_Query( $args );

To add to this, the field is a custom field made with the Advanced Custom Fields plugin, and the date is save by this as yymmdd.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

You can use the meta_query parameter to display all events for the current month like this:

$start_date = date( 'ym01' );
$end_date = date( 'ymt' );
$meta_query = array(
    'key'       => 'event_date',
    'value'     => array( $start_date, $end_date ),
    'compare'   => 'BETWEEN',
    'type'      => 'NUMERIC'
);
$args = array(
    'post_type'         => 'hh_event',
    'posts_per_page'    => -1,
    'orderby'           => 'meta_value',
    'meta_key'          => 'event_date',
    'order'             => 'ASC',
    'meta_query'        => array( $meta_query )
);
$the_query = new WP_Query( $args );
share|improve this answer
    
Makes sense to me except for the ym01 amd ymt parameters. What are those? I just plugged this into my code and got no results. –  Steve Dec 30 '12 at 22:00
    
y = two digit year, m = two digit month, t = number of days in the month –  aaronwaggs Dec 30 '12 at 22:11
    
I tested by adding a custom field event_date and adding values like 121230 to represent today (December 30, 2012) –  aaronwaggs Dec 30 '12 at 22:16
    
Of course. Actually, I changed it to Y (4 digit year) and it works. My dates were 20121231. How would I modify this to show the month after the current month instead? Thanks, –  Steve Dec 30 '12 at 22:16
    
$start_date = date( 'ymd', strtotime( 'first day of next month' ) );, $end_date = date( 'ymd', strtotime( 'last day of next month' ) ); –  aaronwaggs Dec 30 '12 at 22:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.