Help with custom query with custom fields
-
I have a query to grab certain posts based on a custom field (a date). The query is supposed to display posts with the custom field date in the future. The problem is that the custom field is stored as m/d/Y rather than Y/m/d. This is an issue when comparing dates because it doesn’t use the year to base if it is in the future or not.
So, what I need to do, is to somehow read the custom field, change the format and then compare the dates.
My current query
<?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; $today = date('Y/m/d', strtotime('-6 hours')); $myquery = new WP_Query(array( 'post_type' => 'itineraries', 'posts_per_page' => 20, 'paged' => $paged, 'meta_key' => 'start_date', 'orderby' => 'meta_value', 'order' => 'ASC', 'tax_query' => array( array( 'taxonomy' => 'regions', 'field' => 'slug', 'terms' => 'future' ) ) )); if ($myquery->have_posts()) : while ($myquery->have_posts()) : $myquery->the_post(); ?>So above, I need to convert the meta_key start_date to the Y/m/d format for better comparison. How do I do this using code?
I did some research and
implode(‘-’, array_reverse(explode(‘/’,$date)));seems like it might be helpful but I’m not sure how to incorporate it in.Thanks for any help
The topic ‘Help with custom query with custom fields’ is closed to new replies.