4

What is the right URL to retrieve posts by meta_query with the WP-REST-API?

The custom field I want to use can contain multiple values, I tried this for custom field which only can contain a single value and this works.

wp-json/posts?format=json&filter[meta_key]=content_type&filter[meta_value]=2

But I can't get it work with a custom field which can contain multiple values (array/object). Anyone?

colin
  • 425
  • 2
  • 5
  • 17
  • I've added these lines of code to accept meta_key & meta_value as parameters in GET request: add_filter( 'json_query_vars', 'slug_allow_meta' ); function slug_allow_meta( $valid_vars ) { $valid_vars = array_merge( $valid_vars, array( 'meta_key', 'meta_value' ) ); return $valid_vars; } But without any positive result though.. – colin Apr 05 '15 at 17:23
  • It's working now with the un-official json-api. Based on this thread: http://wordpress.stackexchange.com/questions/147723/how-do-you-get-posts-by-meta-query-using-the-json-api-plugin But I really want to use the official WP-REST-API. Hint: The multiple value meta_key contains a serialized array. – colin Apr 06 '15 at 15:18
  • Ok I solved it with the official WP-REST-API. See: http://wordpress.stackexchange.com/questions/169408/wp-json-rest-api-ryan-mccue-how-to-query-posts-with-specific-meta-data-with-a – colin Apr 07 '15 at 15:00

1 Answers1

0

You need the ACF to REST API plugin. In addition to that, you need to make a few customizations such as:

add_filter( 'rest_{type}_query', function( $args ) {
    $args['meta_query'] = array(
        array(
            'key'   => 'my_field',
            'value' => esc_sql( $_GET['my_field'] ),
        )
    );

    return $args;
} );

See https://github.com/airesvsg/acf-to-rest-api/issues/122#issuecomment-291913932

Alin Pandichi
  • 955
  • 5
  • 15