wp query - Order by Date Custom Field

admin2025-06-05  2

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;
}

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;
}
Share Improve this question edited Dec 16, 2018 at 13:33 Max Yudin 6,3982 gold badges26 silver badges36 bronze badges asked Dec 12, 2018 at 12:54 David BarclayDavid Barclay 58 bronze badges 6
  • Remove 'order' => 'ASC',); – Gazi Commented Dec 12, 2018 at 13:05
  • please try : 'orderby' => 'date', – vikrant zilpe Commented Dec 12, 2018 at 13:05
  • Please explain the issue better. – Nicolai Grossherr Commented Dec 12, 2018 at 13:13
  • @Gazi this display the months in the wrong order. – David Barclay 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
 |  Show 1 more comment

1 Answer 1

Reset to default 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');
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749094346a316314.html

最新回复(0)