ACF order date not working

I am looking at another post but haven’t found anything, I have been since 3 days:

I want to display 3 "evolutions" in order ASC but 2018 is always up to 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' => '>=',
        ),
    )
    )
);

?>

Does anyone have an idea?

0
source share
3 answers

I solve the problem with 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
source

I updated your code. Please try an 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 replace the orderby value and add the 'type' parameter to meta_query.

Hope this can be helpful for you.

0
source

Y-m-d,

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

^ ^ functions.php.


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

orderby meta_value?

, !

0

Source: https://habr.com/ru/post/1016413/


All Articles