3

This is similar to, but more specific than, the question found here. That question suggests the Wordpress WP_Query example, which are helpful, but they're not working for me.

My code is as follows:

    $posts = get_posts(array(
        'post_type'         => 'events',
        'posts_per_page'    => -1,
        'meta_key'          => 'from_datetime',
        'meta_value'        => date( "F d, Y g:i a" ), 
        'meta_compare'      => '>',
        'orderby'           => 'meta_value',
        'order'             => 'ASC'
    ));

The custom fields are made using ACF in our blog. The from_datetime field shows like this:

April 18, 2017 2:30 pm

So I've translated this to the date nomenclature:

date( "F d, Y g:i a" )

Events are a custom post type. Basically I want to show the upcoming 2 events that are NOT passed. So this is quite close to the example on the WP_Query page, but still the above doesn't work. The query returns nothing. I do know that if I remove the meta_value stuff above, there are four events to show that are beyond "now".

Any thoughts on what I may be doing wrongly? The query seems correct according to the documentation at WP. Thank you!

  • I would note that this uses WP_Query internally, it may simplify debugging to use that instead by removing several quirks of get_posts. I would also note that meta queries are expensive, and queries that specify unlimited posts don't scale. Set a very high number you would never expect to reach, such as 100 or 200, rather than -1 – Tom J Nowell Mar 27 '17 at 0:08
2

I think you have error in your query. Please try this:

$posts = get_posts(array(
    'post_type'         => 'events',
    'posts_per_page'    => -1,
    'meta_query'        => array(
        'meta_key'          => 'from_datetime',
        'type'              => 'DATETIME',  // You can also try changing it to TIME or DATE if it doesn't work
        'meta_value'        => date( "F d, Y g:i a" ),
        'meta_compare'      => '>',
    ),
    'orderby'           => 'meta_value',
    'order'             => 'ASC'
));
  • I think the -> in your recommended code should be =>. This is after meta_query. Either way, I tried it, and it yields 0 results. Which is incorrect. Any other ideas? – PKHunter Mar 27 '17 at 0:11
  • 1
    You're right, That -> was a mistake. However, I've edited the answer a little. Please try again with this version and see if anything happens. – Abdul Awal Mar 27 '17 at 0:19
  • Thank you @Abdul. This works. However, what if I need to limit this to just the top 2 posts? Putting posts_per_page as 2 does not work. Perhaps because the "first 2" posts are NOT matching that meta_query condition? It's not a showstopper because I can get all the matching posts by leaving the count as -1, and then do a counter inside the foreach loop, and if the counter is > 2, then just break. But this is not elegant. Welcome any thoughts! – PKHunter Mar 27 '17 at 5:22
2

Are you using ACF Pro (e.g., using it's "Date and Time Picker" field type), or one of the plugins that adds a date/time field type to "normal" ACF (e.g., Date & Time Picker for Advanced Custom Fields).

If the former, it stores date/time values in $wpdb->postmeta like 2017-04-18 14:30:00, regardless of the "Display Format" or "Return Format" specified in the field definition. Thus, you should query as:

$posts = get_posts(array(
    'post_type'         => 'events',
    'posts_per_page'    => -1,
    'meta_key'          => 'from_datetime',
    'meta_value'        => date( "Y-m-d h:i:s" ), 
    'meta_compare'      => '>',
    'orderby'           => 'meta_value',
    'order'             => 'ASC'
));

If you're using one of the plugins that adds a date/time field type to "normal" ACF, you'll have to figure out how that plugin stores values and format meta_value accordingly.

  • Thank you @Paul. ACF's field is structured to store date in exactly that form, the one I included in my code. At least in the custom field definition screen. – PKHunter Mar 28 '17 at 7:04
  • 1
    @PKHunter Then you must be using one of the plugins that adds a date/time field type to ACF non-Pro, because as it says in the ACF Pro documentation, "The value selected can be returned and displayed in different formats but will always be saved to the DB as ‘Y-m-d H:i:s’. This format is used throughout the WP database tables and will allow for easy database querying." Which means I have a typo in my answer (should be 'H' instead of 'h'). – Paul 'Sparrow Hawk' Biron Mar 28 '17 at 13:42
  • that may well be true. Let me try.. – PKHunter Mar 29 '17 at 9:44

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.