Support

Account

Home Forums General Issues How to filter products with ACF in rest API Reply To: How to filter products with ACF in rest API

  • Hello @jarvis,
    Thanks for your help.
    I use this code :

    function my_pre_get_products( $query ) {
    	// do not modify queries in the admin
    	if( is_admin() ) {
    		return $query;
    	}
    	// only modify queries for 'product' post type
    	if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'product' ) {
    		// allow the url to alter the query
    		if( isset($_GET['id_article_mercator']) ) {
        		$query->set('meta_key', 'id_article_mercator');
    			$query->set('meta_value', $_GET['id_article_mercator']);
        	}
    	}
    	return $query;
    }
    add_action('pre_get_posts', 'my_pre_get_products');

    Now I have a response on this url : https://mywebsite/wp-json/wc/v3/products?id_article_mercator=42771

    Now I want to have the same result for a category of the products.
    This url returns all the categories.
    https://mywebsite/wp-json/wc/v3/products/categories/

    I want to be able to filter a custom field added by ACF
    https://mywebsite/wp-json/wc/v3/products/categories/?myfield=testvalue

    Do you have an idea what to add as function for this ?
    What’s strange also, for the products, I can see in the rest api result the metadata with the values added by ACF
    For the category, the metadata aren’t visible.
    In the database the values are stored in termmeta for the categories and in postmeta for the products.
    If I’m right, the product is a post and the category is a taxonomy.

    Thanks in advance,