• Resolved mbujes

    (@mbujes)


    Hello, I am having problems ordering users by an ACF datepicker field.

    Datepicker return format is Ymd.

    I don’t have problems ordering fields that aren’t a date.

    My code is:

    
    function order_rest_user_query($query_vars, $request) {
            $orderby = $request->get_param('orderby');
            if (isset($orderby) && $orderby === 'activo') {
                $query_vars["orderby"] = "meta_value";
                $query_vars["meta_key"] = "activo";
            }
            if (isset($orderby) && $orderby === 'fecha_alta') {
                $query_vars["orderby"] = "meta_value_num";
                $query_vars["meta_key"] = "fecha_alta";
            }
            return $query_vars;
        }
        
        add_filter( 'rest_user_query', 'order_rest_user_query', 10, 2);
    

    The endpoint I am using is:

    https://xxxxx/wp-json/wp/v2/users?context=edit&page=1&per_page=10&orderby=fecha_alta&order=asc

    I have tested a lot of things with different order as result but aren’t right:

    A lot of thanks.

    Regards

Viewing 2 replies - 1 through 2 (of 2 total)
  • Orange

    (@leegrant)

    Hi,

    Did you ever figure this one out? I’m having the same issue at the moment.

    Thanks

    Thread Starter mbujes

    (@mbujes)

    Yes, I solved the issue. You have to see with phpmyadmin the format you have stored the dates in the database and they are in the same format.

    I had many records stored in different formats so the ordering went wrong.

    Regards

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘REST API – Ordering by ACF Date field’ is closed to new replies.