pre_get_posts and custom field values
-
Hi
I am trying to use pre_get_posts to alter a query for a custom post type. I would like to order the results by the values of a custom field. My custom field is called “event_start_date” and a typical value is “02/12/2012 19:00”. The format of the string is dd/mm/yy h:i
Is this possible using pre_get_posts and custom field values?
The trouble I have is that the custom field holds this type of string value, and not a proper time stamp. So doing comparisons etc is difficult.
Here is what I have cobbled together so far. However it is not ordering these posts by their ‘date’ order…
function my_event_query( $query ) { if( is_post_type_archive( 'events' ) ) { $meta_query = array( array( 'key' => 'event_start_date', 'value' => time(), 'compare' => '>' ) ); $query->set( 'meta_query', $meta_query ); $query->set( 'orderby', 'meta_value_num' ); $query->set( 'meta_key', 'event_start_date' ); $query->set( 'order', 'ASC' ); $query->set( 'posts_per_page', '-1' ); } } add_action( 'pre_get_posts', 'my_event_query' );
Is there any possibility of extracting the day/month/year time components so that I can order by date and time?
Thanks in advance
Rich
- The topic ‘pre_get_posts and custom field values’ is closed to new replies.