• Hi,

    I’m having trouble to sort events by startdate.

    I have events with a ticket system plugin event tickets plus, every ticket has a date. I’m displaying the events 0,10 per genre and the order has to be by first ticket date after todays date.

    The events are in the wp_posts table, the tickets are in the wp_postmeta table:

    meta_id => #,
    post_id => #,
    meta_key => 'movie_time_values',
    meta_value = > ',{"theatre":"","<strong>date</strong>":"<em>05.28.2016</em>","time":"20:00","place":"","ticket":"","button_text":"","button_link":""}'

    Right now I’m stuck with the following, it gives no result:

    global $query_string;
    
    	$today = current_time('m.d.Y');
    
    	$args = array(
    		'numberposts' => -1,
    		'post_type' => 'movies',
    		'event_categories' => '',
    		'posts_per_page' => -1,
    		'meta_key' => 'movie_time_values',
    		'orderby' => 'meta_value',
    		'order' => 'ASC',
    		'meta_query'	=> array(
    			'relation'		=> 'AND',
    			array(
    				'key'		=> 'movie_time_values',
    				'value'		=> $today,
    				'compare'	=> '>='
    			)
    		)
    	);
    
    	$args = array_merge( $args , array_filter( $wp_query->query_vars ) );
    	query_posts( $args );
    
    	/* Start the Loop */
    	while ( have_posts() )
    	{
    		the_post();
    .......

    There is a result when i delete the following in above code:

    'meta_key' => 'movie_time_values',
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'meta_query'	=> array(
    'relation'		=> 'AND',
    	array(
    	'key'		=> 'movie_time_values',
    	'value'		=> $today,
    	'compare'	=> '>='
    ))

    And this gives the following query:

    SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)  INNER JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id) JOIN wp_icl_translations t
    							ON wp_posts.ID = t.element_id
    								AND t.element_type = CONCAT('post_', wp_posts.post_type)  WHERE 1=1  AND (
      wp_term_relationships.term_taxonomy_id IN (31)
      AND
      tt1.term_taxonomy_id IN (31)
    ) AND wp_posts.post_type = 'movies' AND (wp_posts.post_status = 'publish') AND ( ( t.language_code = 'nl' AND wp_posts.post_type  IN ('post','page','attachment','ticket-meta-fieldset','tribe_rsvp_tickets','mc4wp-form','movies','special_offers','tribe_wooticket' )  ) OR wp_posts.post_type  NOT  IN ('post','page','attachment','ticket-meta-fieldset','tribe_rsvp_tickets','mc4wp-form','movies','special_offers','tribe_wooticket' )  ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10

    Thanks in advance

Viewing 3 replies - 1 through 3 (of 3 total)
  • How are your times stored?

    It could be as simple as changing your orderby from meta_value to meta_value_num

    Also, it’s vital to be sure that your dates/times are stored in the same format as your $today variable….it looks like you’re comparing times (in your metatdata array) to a date (in your $today variable).

    I do something very similar on a site for a wilderness non-profit that leads guided hikes – they only want to show upcoming not past hikes (except that I’m working with a single date, not an array of times), so I structure my query like this:

    global $post;
    $args = array (
       'post_type' => array ('hike','event'),
       'meta_key' => 'hike_date_start',
       'orderby' => 'meta_value_num',
       'order' => 'ASC',
       'posts_per_page' => -1);
    
    $myposts = get_posts($args);
    foreach ( $myposts as $post ) : setup_postdata( $post );
    
    $exp_date = strtotime(get_post_meta($post->ID,'hike_date_start',true));
    $today = time();
    if ($today <= $exp_date) {

    Notice that I use strtotime to put the metadata into the same format (a timestamp) as the $today variable (time(); gives you the current date in a timestamp format).

    It might be that you need to convert your movie start times to timestamps first, then do your compare.

    Remember also that to fetch and display metadata you need global $post, but since you’re using the “while” loop instead of the “foreach” way of looping, you don’t need to setup_postdata, so you can ignore that part from my code.

    I hope this helps!

    Thread Starter john2016

    (@john2016)

    TrishaM, thank you for your answer.

    The dates are stored in this format: m.d.Y

    But they are stored in the custom field movie_time_values together with some other values. Example value:
    ',{"theatre":"","date":"05.28.2016","time":"20:00","place":"","ticket":"","button_text":"","button_link":""}'

    So I need to sort the value of “date” ascending, in above example with value: 05.28.2016.

    I am also struggling with some values in this code:

    'meta_key' => 'movie_time_values',
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'meta_query'	=> array(
    'relation'		=> 'AND',
    	array(
    	'key'		=> 'movie_time_values',
    	'value'		=> $today,
    	'compare'	=> '>='
    ))

    I don’t know if relation is needed.
    And i think value needs to be an array, because i need to point to the value of the date portion..

    Last night i was trying to get the post_id and date (strtotime) together in one array. Then reorder the array using ksort.

    I was able to create the following array, but i wans’t able to sort on date. And after that i don’t know how to loop through the posts with the new ordered post_id list:

    Array
    (
        [0] => Array
            (
                [1475193600] => 2067
            )
    
        [1] => Array
            (
                [1475280000] => 2070
            )
    
        [2] => Array
            (
                [1475280000] => 2078
            )

    By any chance are you using Advanced Custom Fields? From the looks of how your metadata is stored, it looks like ACF fields with repeaters or rows…..data entered is stored in an array.

    If that’s the case, ACF makes it fairly simple to retrieve and parse that data using the “get_field” function and then sorting through the row data to get just what you want.

    Here’s an example:
    https://www.advancedcustomfields.com/resources/get_sub_field/

    If you’re not using ACF, then there is still a way to do this, but you will likely need to unserialize the array of data first,

    $myvalues = unserialize(get_post_meta($post->ID,'my_key',true)); //using 'true' here is vital
    foreach ($myvalues as $myvalue) { echo $myvalue.', '; }

    This will just echo what you have, so you can figure out how to work with it and what piece you need to target to get your dates and times to match – I do still think you’ll have to convert to timestamps to do a compare, but try it without first.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘order by meta_value where value contains array with date’ is closed to new replies.