0

I'm trying to display a list of upcoming football matches in order of kick off. The code I currently have lists the dates in the correct order but the posts ordering is incorrect.

<?php
$previews_new_loop = array(
    'posts_per_page' => -1,
    'post_type'      => 'football_match',
    'meta_key'       => 'kick-off-date',
    'orderby'        => 'meta_value',
    'order'          => 'ASC',
);

$wpquery = new WP_Query( $previews_new_loop );   
$posts = $wpquery->get_posts();
$ordered_posts = array();

foreach ( $posts as $post ) {
    $meta_value = get_post_meta( $post->ID, 'kick-off-date', true );
    if ( !$meta_value ) {
        continue;
    }
    $date = date( 'ddmmyyyy', strtotime( $meta_value ) );
    $ordered_posts[$date][] = $post;
}

foreach ( $ordered_posts as $post_date => $posts ) {
    foreach ( $posts as $post ):
    endforeach;
}
6
  • Remove 'order' => 'ASC',);
    – Gazi
    Commented Dec 12, 2018 at 13:05
  • please try : 'orderby' => 'date', Commented Dec 12, 2018 at 13:05
  • Please explain the issue better. Commented Dec 12, 2018 at 13:13
  • @Gazi this display the months in the wrong order. Commented Dec 12, 2018 at 13:17
  • Dates have to stored as yyyymmdd. they’re just compared as 8-digit numbers, the units need be in descending order.
    – Milo
    Commented Dec 12, 2018 at 13:55

1 Answer 1

0

I have the code below (started from yours) setup as a shortcode and it works fine. When I change the direction 'ASC' to 'DESC', the sort order changes as well, so it is not 'luck' that posts are in the correct order. It assumes that the dates are stored yyyymmdd. NOTE that one must remove the line:

$date = date('ddmmyyyy', strtotime($meta_value)); 

if the dates are already stored ddmmyyyy.

function sort_by_date() {
        $previews_new_loop = array(
    'posts_per_page' => -1,
    'post_type' => 'article', //'football_match',
    'meta_key' => 'kick-off-date',
    'orderby' => 'meta_value',
//  'order' => 'ASC',);
'order' => 'DESC',);

    $wpquery = new WP_Query($previews_new_loop);   
    $posts = $wpquery->get_posts();  
    $ordered_posts = array();
    foreach ($posts as $post) {
        $meta_value = get_post_meta($post->ID, 'kick-off-date', true);
        if (!$meta_value) {
            continue;
        }
        //$date = date('ddmmyyyy', strtotime($meta_value));
        $ordered_posts[$meta_value][] = $post;
    }
    $html = '<pre>'.print_r($ordered_posts, true).'</pre>';
    return $html;
}

add_shortcode('sort_by_date','sort_by_date');
4
  • thank you for this, however making the above changes does not separate each each date out which I need. I would liketo group each set of events by date with the date as the heading. Commented Dec 13, 2018 at 13:47
  • Ok now that the ‘order by’ has been solved you can do the rest. I just did a debug style dump to check the order. You had the beginnings right for your grouping by date. Give it a go at structuring the HTML the way you want. If you get stuck with that, start a new question as it is different from the ordering question.
    – anmari
    Commented Dec 13, 2018 at 21:42
  • that was the issue I was having, the event order was wrong compared to the date grouping. Commented Dec 14, 2018 at 15:06
  • So you ok now right? For html use something like: $html = PHP_EOL.'<ul>'; foreach ($ordered_posts as $kickoff => $events) { $html .= PHP_EOL.'<li>'.$kickoff.PHP_EOL.'<ul>'; foreach ($events as $event) { $html .= PHP_EOL.'<li>'.$event->post_title.'</li>'; } $html .= PHP_EOL.'</ul>'.PHP_EOL.'</li>'; } $html .= PHP_EOL.'</ul>';
    – anmari
    Commented Dec 15, 2018 at 3:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.