Dynamic date variables for custom post type query

I am querying a custom post type called 'events' which has a meta field called 'event_date' which is stored in date format. It is my desire to have three or four separate queries each looking for posts within specific time periods. The first query will be all posts within the current month, then another for the following month, then the month there after, and so on. It is important that they are separate queries and not one.

I am able to query between set dates using the code below but I wish to make these dynamic and be able to add a start date variable and an end date variable based upon the current date. I was thinking of using something similar to this code I came across which does successfully give me the month name but am struggling to use this within my WordPress query.

<?php
$d = new DateTime( 'now' );
$d->modify( 'first day of this month' );
echo $d->format( 'F' ), "\n";
?>

Is there anyway I can integrate dynamic start date date and end date variables which can be current month, +1, +2, +3months, etc as required? I appreciate any help you can provide.

My query code is:

<?php
$args = array( 
    'post_type' => 'event',
    'meta_key' => 'event_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'showposts' => 20,
    'meta_query' => array(
    'relation' => 'AND',
        array(
            'key' => 'event_date',
            'value' => '2014-06-01', // Lowest date value
            'compare' => '>='
        ),
        array(
            'key' => 'event_date',
            'value' => '2014-06-31', // Highest date value
            'compare' => '<='
        )
    )
);
$event_posts = new WP_Query($args);

if($event_posts->have_posts()) : 
while($event_posts->have_posts()) : 
  $event_posts->the_post();
?>

POST CONTENT HERE

<?php
    endwhile;
    else: 
?>

Oops, there are no posts.

<?php
    endif;
    ?>

Answers


Have a look at http://www.php.net/manual/function.mktime.php. You can use something like this:

$month_add = 0; // current month, add value for the next months
$low_date = date("Y-m-d", mktime(12, 0, 0, date("n")+$month_add, 1, date("Y")));
$high_date = date("Y-m-d", mktime(12, 0, 0, date("n")+$month_add+1, 1, date("Y")));

Change compare for highest date to:

'compare' => '<'

use this syntax... easy to read and use:

array(
        'key' => 'event_date',
        'value' => date('Y-m-d',strtotime('+1 month')), 
        'compare' => '<='
    )

have fun playing with it and check out Relative formats for all your possibilities


Need Your Help

Non repeating content change for wordpress using add_filter

wordpress preg-replace

I want to put a pdf icon in for pdf links automatically in wordpress

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.