• I want to filter posts based on multiple acf custom fields with AND relation. Something like this:

    $args = array(
            'post_type'  => 'product',
            'meta_query' => array(
                'relation' => 'AND',
                array(
                    'key'     => 'color',
                    'value'   => 'blue',
                    'compare' => '=',
                ),
                array(
                    'key'     => 'price',
                    'value'   => array( 20, 100 ),
                    'type'    => 'numeric',
                    'compare' => 'BETWEEN',
                ),
            ),
        );

    I might even have more filters. How can I convert these to REST API 2 filters?

    https://www.ads-software.com/plugins/rest-api/

Viewing 1 replies (of 1 total)
  • Hi sohrabtaee,
    sorry for the late reply. Basically speaking, custom fields are not available for filtering through the WordPress REST API right now, since this opens up some implementation and security issues depending on the data you store as meta data.

    Nevertheless, you could work around by registering the WordPress Meta Query as a query var for the REST API:

    add_filter( 'rest_query_vars', 'test_query_vars' );
        function test_query_vars ( $vars ) {
            $vars[] = 'meta_query';
            return $vars;
        }

    After this you could create a URL like

    https://local.wordpress.dev/wp-json/wp/v2/posts?filter[meta_query][0][key]=color&filter[meta_query][0][value]=blue&filter[meta_query][1][key]=price&filter[meta_query][1][value][0]=20&filter[meta_query][1][value][0]=100&filter[meta_query][1][type]=numeric&filter[meta_query][1][compare]=BETWEEN

    Have also a look into this related discussion here:
    https://github.com/WP-API/WP-API/issues/2459

Viewing 1 replies (of 1 total)
  • The topic ‘Filtering multiple custom fields with WP REST API 2’ is closed to new replies.