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

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.

Leave a Comment