AnswerBun.com

WP REST API: Order posts by meta value (acf)?

WordPress Development Asked by Mauro Bringolf on December 9, 2020

I’m requesting posts with the WP REST API and need to sort them according to an ACF field. It’s value represents a date (numeric, jQuery date format yymmdd). I know how to do it with a normal WP_Query and tried to do the same using the rest api:

mydomain.com/wp-json/wp/v2/posts?filter[orderby]=meta_value_num&filter[meta_key]=my_field_name&filter[order]=DESC

In fact I’m using a custom post type that’s registered with the rest api and everything else is working perfectly, so I think its not a cpt specific issue?

But the posts show up in default order (their creation date, latest to oldest). What am I missing? Is this orderby parameter not supported by the rest api? If so, how can I implement it myself?

Any other workarounds, suggestions? Really looking for a solution! Thankful for any hints!

2 Answers

I'm guessing you haven't exposed meta_key and meta_value to the REST API with the rest_query_vars filter, so this should do it:

function my_add_meta_vars ($current_vars) {
    $current_vars = array_merge ($current_vars, array ('meta_key', 'meta_value'));
    return $current_vars;
}
add_filter ('rest_query_vars', 'my_add_meta_vars');

Then you can refer to meta_key and meta_value in your query.

Be aware that this obviously exposes all your post metadata to the API, which has potential security implications; I believe that's why it's not activated by default.

Correct answer by iguanarama on December 9, 2020

add_filter( 'rest_post_query', function (  $args, $request ) {
    if ( isset( $request['meta_key']) && !empty($request['meta_key'] ) ) {
        $args['meta_key'] = $request['meta_key'];
    }
    return $args;
}, 10, 2);

add_filter( "rest_post_collection_params", function($query_params, $post_type){

    array_push($query_params['orderby']['enum'],'meta_value' );

    return $query_params;
}, 10, 2);

Answered by Amjad on December 9, 2020

Add your own answers!

Related Questions

List posts based on first letter of posts

2  Asked on September 13, 2020 by shahinul-islam

   

How to redirect a page to another?

1  Asked on September 13, 2020 by heera

     

How to get the meta title of a page configured as blog (loop)

1  Asked on September 8, 2020 by gerard

 

I am trying to match the wpallimport using the Xpath Filter

1  Asked on September 4, 2020 by ankit-prajapati

     

how to list all post that are in the custom taxonomy using $wpdb

0  Asked on September 3, 2020 by kenneth-gervacio

   

How to pass multiple values in a form for tax_query?

0  Asked on August 21, 2020 by irishrunner16

   

How to add post_distinct filter to WP_Comment_Query?

2  Asked on August 17, 2020 by jonathan-gruber

   

How can I combine one field using wpdb and group by?

1  Asked on August 10, 2020 by user81828

   

Custom posts password protect

1  Asked on August 6, 2020 by jason

   

REST API: Display Category names in JSON?

1  Asked on August 3, 2020 by steve

       

Gutenberg withInstanceId. When to use it?

2  Asked on July 25, 2020 by at-least-three-characters

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP