• Hi,

    I’m currently coding an event plugin and want to display the events. However, when I get them to display, it shows all the events in the order of when they were created and, even past events.
    What I want is to be able to order the posts so that the next event is first and so that no events which have passed are shown.
    My issue is that the date is stored in a meta_key called ‘WooCommerceEventsDate’ as ’12 February 2016′ for example.

    Following a tutorial from another forum article, I came up with this but it doesn’t show anything.

    $args = array(
    	'post_type' => 'product',
    	'posts_per_page' => 1,
    	'post_status' => 'publish',
    	'meta_key'=>'WooCommerceEventsDate',
    	'orderby' => 'meta_value_num',
    	'order' => 'DESC',
    	'meta_query' => array(
    		array(
    			'value'         => date('j F Y',strtotime("today")),
    			'compare'       => '>=',
    			'type'          => 'DATE'
    		)
    	)
    );

    Anyone got any ideas?

Viewing 2 replies - 1 through 2 (of 2 total)
  • ‘post_type’ => ‘product’ is a WooCommerce custom post type. If you are working with events, then the post_type would be something different. Without having access to your site I would not be able to really troubleshoot this but it looks as if this code was designed to run against a WP site with WooCommerce installed and is a way of sorting products by date. There is nothing about events here.

    AJ

    Moderator bcworkz

    (@bcworkz)

    If one were selling tickets to events, it’s conceivable that events are managed as Woo products. Determining the proper post type shouldn’t be too difficult. The greater difficulty I see is applying less than/greater than logic to date strings. Such comparisons use alphabetical logic, not numeric. The danger is sometimes the comparisons work, other times they do not.

    Ideally the date values should be stored as UNIX timestamps, then any arithmetic applied will work correctly. The timestamps are easily converted to human readable form with date(). If you must use date strings in comparison queries, there are SQL conversion functions, but to use them you need to compose custom mySQL queries, you cannot use WP_Query unless the function code is inserted by filter.

    The other alternative is not nearly as efficient but may be easier to code. That is to query for all otherwise applicable events and suppress out of date events within the loop itself.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WP_Query ordered by a date which is held as a meta key’ is closed to new replies.