2

I have a post type called "agenda" and one of its meta value is the date of the event. How can I get the posts of the current year based on the meta value?

I tried this args above but it doesn't work.

$args = array(  'post_type' => 'agenda',
                'posts_per_page' => -1,
                'sort_order' => 'asc',
                'meta_query' => array(
                    array(
                        'key' => 'date_event',
                        'value' => date('Y'),
                        'compare' => '=',
                        'type' => 'DATETIME'
                    )
                ),
                'meta_key' => 'date_event',
                'meta_type' => 'DATE',
                'orderby' => 'meta_value',
                'order' => 'ASC',
                'tax_query' => array(
                    array(
                    'taxonomy' => 'event_type',
                    'field' => 'term_id',
                    'terms' => $type_name)
                )
);
  • Maybe test to set the type of your meta query to DATE instead of DATETIME? – Friss Mar 29 '18 at 21:31
3

Depends what you are getting back from the 'date_event' post-meta field.

That's probably the culprit here -- if it's an actual date with a month and day, it's not going to match what you are getting from date('Y'), which is just going to be '2018'.

You can check the value in 'date_event' on any of the agenda posts with:

print_r( get_post_meta( $post_id, 'date_event', true ) );

If the value in 'date_event' is a Unix timestamp, that actually makes it easy because then you can just query for all dates starting with Jan 1 of the current year, something like:

$first_of_year = strtotime( "1 January " . date( 'Y' ) );

$args = array(  'post_type' => 'agenda',
    'posts_per_page' => -1,
    'meta_key' => 'date_event',
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'meta_query' => array(
        array(
            'key' => 'date_event',
            'value' => $first_of_year,
            'compare' => '>=',
        ).
    ),
    'tax_query' => array(
        array(
           'taxonomy' => 'event_type',
           'field' => 'term_id',
           'terms' => $type_name,
        )
    )
);

If date_event is giving you a value like 20180320, you could do something similar, but with ...

$first_of_year = date( 'Y' ) . '0101';

If you are getting something like '2018-03-29' that's when you might need to use the 'type' => 'DATE' -- but if you have one of the first two situations, you don't need it.

One other thing: 'sort_order' isn't a valid param, though it's just going to be ignored). You just want 'order', which you had lower down anyway.

Ans when you are comparing numeric values it's best practice to change 'orderby' => 'meta_value' to 'orderby' => 'meta_value_num'.

  • What I'm getting back is a data format in d/m/Y. Actually what I did is explode the string and compare value of the array with the current year. foreach($agendas as $agenda) { $data = explode('/', get_field('data_do_evento',$agenda->ID)); if( $data[2] === date('Y')) { $result[] = $agenda; } } But I'll try to the those posts straight by args. Thanks – Thales Andrade Mar 29 '18 at 22:24
  • Yeah, I think in that case you might not be able to do it with the query args and have to grab everything and then loop as you did. Might be another way with that 'meta_type' parameter. I'm not super familiar with how that works. Conversely you might look at changing how you store the date -- then you just use date() to format it for display on the front end. – slashbob Mar 29 '18 at 23:32
  • Anyway I'll the way you showed. Seems to be more as a best practice. Thank you so much bro. – Thales Andrade Mar 30 '18 at 7:38

Your Answer

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

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