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 custom field which stores a date in a timestamp. I want to run a query that display posts based on the month, for example all entries from March, the specific day or year doesn't matter. I'm guessing i need to use the DATE or the DATETIME type for the meta query, but i don't know how to proceed:

if ($_GET['month']) {
    $meta_query[] = array(
            'key' => 'event_start_date',
            'value' => $_GET['month']
    );
}

$args = array(
    'post_type' => 'event',
    'paged' => $paged,
    'posts_per_page' => -1,
    'tax_query' => $cleanArray,
    'meta_key' => 'event_start_date',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    'meta_query'=> $meta_query
);

$events = new WP_Query($args);
share|improve this question
1  
I don't think you'll be able to do this with just WP_Query, you'll need a filter on posts_where to query on just the month using MySQL's month function. –  Milo Oct 14 '13 at 16:10

1 Answer 1

up vote 1 down vote accepted

You need the 2 digit month number that you want to query on and then use the code below. This should be easy with php (for example, see this post). In the code below $month is the number of the month in a 2 digit format, eg March would be 03.

$start_date = date('Y'.$month.'01'); // First day of the month
$end_date = date('Y'.$month.'t'); // 't' gets the last day of the month

$meta_query = array(
    'key'       => 'event_start_date',
    'value'     => array($start_date, $end_date),
    'compare'   => 'BETWEEN',
    'type'      => 'DATE'
);
share|improve this answer

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.