Support » Fixing WordPress » Order loop by custom field date

  • Hi there
    I’m struggling to work out how to sort the list below into order by custom field ‘date_start’ – to output a list of “coming soon” shows per theatre that are in the correct date order. The web page is:

    $today = date('Y-m-d');
    $args = array('cat' => 12, 'meta_key' => 'theatre_id', 'meta_value'	=> $post->ID, 'meta_query' =>array
    ('relation' => 'AND',
    array('key' => 'date_start', 'value' => $today, 'compare' => '>' ), 		), );
    $the_query = new WP_Query($args);
    <?php if($the_query->have_posts() ) : ?>
    <div class="headlineexcerpt3" style="display:inline;">
    Coming Soon:
    <?php // The Loop
    while ( $the_query->have_posts() ) : $the_query->the_post();
    $showid = $the_query->post->ID; ?>
    <li><a href="<?php the_permalink();?>"><?php echo get_post_meta($showid, 'showname',1); ?></a></li> 
    <?php endwhile; wp_reset_postdata(); ?>
    <div style="clear:both;"></div>
    <?php endif;//COMING SOON query?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • J M


    What does your code currently output?

    It outputs a list of shows playing at the theatre, but orders them by post date, not by my custom field “date_start”

    J M


    You’ll need to use Order parameters in WP_Query.


    'meta_key' => 'date_start',
    'orderby'  => 'meta_value',

    In your query.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Order loop by custom field date’ is closed to new replies.