0

In the context of a Wordpress widget development, I use WP_Query this way:

$args = array(
    'post_type' => 'myType',
    'post_per_page' => $number,
    'showposts' => $number
);
$latest_myTypes = new WP_Query($args);
if ($latest_myTypes->have_posts()):
    global $post; //etc.

When a myType post is saved, it stores some values in the table wp_postmeta, in particular two keys: day (an integer) and month (a string).

As you can see, in the preceding code I get all the posts of type myType.

I need a different thing: since any single post contains upcoming events, I want to get all the posts where the date is equal or after the current date (so, day must be at least today AND the month should be at least the current month).

Is it possible to modify my WP_Query to filter the posts this way?

I've red about meta_query but I've not understood how to integrate a meta query to my current query, and even how to create the query itself.

In pseudocode, I should get all the posts WHERE:

day >= date("j")

    AND

date("n", strtotime(month)) >= date("n")

Can you help me?

Update: I know it would be much easier if I could store the Unix timestamp of an upcoming event date: problem is I inherited code from another developer and found that he used metaboxes to automatically save data. I couldn't find a way to alter his code to save day, month and year submitted by a user as a combined and unique key value, since he uses three different fields (it would be interesting if I could create a sort of computed field)

Update: I suppose I solved this way:

$args = array(
        'post_type' => 'myType',
        'post_per_page' => $number,
        'showposts' => $number,
        'meta_query' => array(
            array(
                'key'       => 'month',  
                'value'     => array(date("F"),date("F", strtotime('+1 month'))),
                'compare'   => 'IN'
            ),
            array(
                'key'       => 'day',  
                'value'     => date("j"),
                'compare'   => '>='
            ),
            array(
                'key'       => 'year',  
                'value'     => date("Y"),
                'compare'   => '='
            ),
        ),
    );

It is working fine. Any other comment, solution, suggestion, etc. is eventually appreciated.

0

This is the answer to my question:

$args = array(
        'post_type' => 'myType',
        'post_per_page' => $number,
        'showposts' => $number,
        'meta_query' => array(
            array(
                'key'       => 'month',  
                'value'     => array(date("F"),date("F", strtotime('+1 month'))),
                'compare'   => 'IN'
            ),
            array(
                'key'       => 'day',  
                'value'     => date("j"),
                'compare'   => '>='
            ),
            array(
                'key'       => 'year',  
                'value'     => date("Y"),
                'compare'   => '='
            ),
        ),
    );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy