Support

Account

Home Forums Front-end Issues Meta query doesn't effect queried posts

Solved

Meta query doesn't effect queried posts

  • Hello there!
    Im trying to query posts of a custom post type by comparing the value of the “Date-TIme-Picker” to the current time – referencing to this article in the resources. However, no matter whether the value in the custom field is before or after the current time, it queries all the posts anyway. As if the meta_query had no effect.

    The PHP:

    $timenow = date('Y-m-d H:i:s');
    
    // WP_Query arguments
    $args = array(
    	'post_type'              => 'konzert',
    	'post_status'            => 'publish',
    	'meta_query'			 => array(
    		'relation' => 'AND',
    		array(
    			'key' => 'datum_und_uhrzeit_ende',
    			'compare' => '<',
    			'meta-value' => $timenow,
    			'type' => 'DATETIME'
    		)
    	)
    );
    
    // The Query
    $query_konzert = new WP_Query( $args );

    And the resulting SQL-query:
    SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( wp_postmeta.meta_key = 'datum_und_uhrzeit_ende' ) AND wp_posts.post_type = 'konzert' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10

    Any help or ideas are much appreciated! Thanks

  • This

    
    'meta-value' => $timenow,
    

    should be

    
    'value' => $timenow,
    
  • Aargh! Sometimes I really hate me. How could I not see that.

    Thank you so so much.

  • Because it is extremely easy to overlook things like this in your own code. Trust me, I do it all the time. It is much easier to see errors like this in other peoples code.

  • Hi – just to clarify (as I have been trying to work out how to do this too) – does this query show only the posts for which the end date/time has not passed yet?

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Meta query doesn't effect queried posts’ is closed to new replies.