I get my posts of an cpt like this:
http://www.mywebsite.com/wp-json/wp/v2/cpt
at this posts there are shown post_meta values.
Does anyone know a way to order the posts by the meta like this:
http://www.mywebsite.com/wp-json/wp/v2/cpt?orderby=my_meta_field
I found some solutions, but none of them are working for me….
Please help!
Thanks! Stefan
2 Answers
You can use this filter to change order :
$type = "cptCode";
add_filter("rest_" . $type . "_query", function ($args, $query) {
$args["orderby"] = "meta_value";
$args["meta_key"] = "my_meta_field";
return $args;
}, 10, 2);
You can also test $_GET
to change the order conditionally.