Orderby ACF custom field date don't work

2020-04-30 02:50发布

问题:

I view the other post but I found nothing, I'm on since 3 days :

I want display 3 'evenements' in the order ASC but 2018 is always before 2017

$auj = date('Ymd');

$queryEvent = new WP_Query(
    array(
    'category_name' => 'evenements',
    'posts_per_page' => 3,
    'meta_key' => 'date_de_fin',
    'orberby' => 'meta_key',
    'order' => 'ASC',
    'meta_query' => array(
        array(
            'key' => 'date_de_fin',
            'value' => $auj,
            'compare' => '>=',
        ),
    )
    )
);

?>

someone has an idea ?

回答1:

I have updated your code .Please try your updated query.

<?php $auj = date('Ymd');

$queryEvent = new WP_Query(
    array(
        'category_name' => 'evenements',
        'posts_per_page' => 3,
        'meta_key' => 'date_de_fin',
        'orberby' => 'meta_value_num', 
        'order' => 'ASC',
        'meta_query' => array(
            array(
                'key'     => 'date_de_fin',
                'value'   => $auj,
                'compare' => '>=',
                'type' => 'DATE'
             ),
        )
    ));
?>

I have replace 'orderby' value & added 'type' parameter in meta_query.

Hope, this may be helpful to you.



回答2:

As your date format is not in Y-m-d so you need to define your own logic for shorting

function wh_posts_orderby($orderby, $query) {
    //Only for custom orderby key
    if ($query->get('orderby') != 'yyyymmdd_date_format')
        return $orderby;
    if (!( $order = $query->get('order') ))
        $order = 'ASC';
    global $wpdb;
    $fieldName = $wpdb->postmeta . '.meta_value';
    return "STR_TO_DATE(" . $fieldName . ", '%Y%m%d') " . $order;
}

add_filter('posts_orderby', 'wh_posts_orderby', 10, 2);

Add the ^^above code to your active theme functions.php file.


Now you can sort your post by date (format YYYYMMDD)

$auj = date('Ymd');
$args = [
    'category_name' => 'evenements',
    'posts_per_page' => 3,
    'meta_key' => 'date_de_fin',
    'orberby' => 'yyyymmdd_date_format', //check this line
    'order' => 'ASC',
    'meta_query' => [
        'relation' => 'AND', //added this
        [
            'key' => 'date_de_fin',
            'value' => $auj,
            'compare' => '>=',
            'type' => 'DATE'
        ],
    ]
];
$queryEvent = new WP_Query($args);
if ($queryEvent->have_posts()) :
    /* Start the Loop */
    while ($queryEvent->have_posts()) : $queryEvent->the_post();

    //you post

    endwhile;
endif;

Related answer How do I query posts and use the 'orderby' attribute to order posts in loop according to date 'meta_value'?

Hope this helps!



回答3:

I resolve by problem with a SQL query :

   $queryEvent = $wpdb->get_results( 'SELECT * FROM cci_posts INNER JOIN cci_postmeta ON cci_posts.id = cci_postmeta.post_id INNER JOIN cci_term_relationships ON  cci_posts.id = cci_term_relationships.object_id WHERE cci_term_relationships.term_taxonomy_id = 4 AND cci_postmeta.meta_key = "date_de_fin" AND cci_postmeta.meta_value > 20170221 AND cci_posts.post_status = "publish" ORDER BY cci_postmeta.meta_value ASC LIMIT 3', OBJECT );
                        //$queryEvent = new WP_Query($args);
                        ?>
                        <?php if ( $queryEvent ) : ?>

                        <?php /* Start the Loop */ ?>
                        <?php foreach ($queryEvent as $post) {
                          setup_postdata($post); ?>