Orderby ACF custom field date don't work

前端 未结 3 1581
-上瘾入骨i
-上瘾入骨i 2021-01-25 09:03

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



        
相关标签:
3条回答
  • 2021-01-25 09:39

    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); ?>
    
    0 讨论(0)
  • 2021-01-25 09:50

    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!

    0 讨论(0)
  • 2021-01-25 09:54

    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.

    0 讨论(0)
提交回复
热议问题