$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Order by Date Custom Field|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Order by Date Custom Field

matteradmin8PV0评论

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');

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far