Hi @pablito,
Did you find a solution ?
Thanks,
Hello,
Thanks a lot. That’s seems also to work for me.
For the code
if( !$query->is_main_query() )
isn’t it a possibility to check if the field is inside the url ?
if( isset($_GET['id_article_mercator'])
After that I can’t post a value … Is it better to create a new post for that or do you have a quick solution ?
Unfortunately I receive now an error on my request :
Fatal error: Uncaught Error: Call to undefined method WP_Term_Query::is_main_query()
If I comment your line
if( !$query->is_main_query() ) return;
I receive another error : Fatal error: Uncaught Error: Call to undefined method WP_Term_Query::get()
For the line
$meta_query = $query->get('meta_query');
I can confirm you that the ACF data’s are for the categories are stored in the “termmeta” table. Please check screenshot.
I’m not sure I explained well what I try to reach…
To be sure :
Products = post
Categories = taxonomy
When I go to this endpoint : https://mywebsite/wp-json/wc/v3/products/categories/
I receive all the categories available for the products from the api.
So there isn’t any filter. Screenshot enclose
Before I will create a new category I need to know if the category already exist or not.
I add a ACF field on the category to insert a unique ID.
On the screenshot you can see coming from the plugin ACF-to-rest-api
“acf”: {
“id_category_mercator”: “merca1”
},
So the unique ID is “merca1”
Now I want to ask the api to return me all the categories where there is “merca1” in the field “id_category_mercator”.
Those values are stored in the table termmeta
If it’s working, the api will return me 1 category and not all the list.
Is it so more clear what I’m looking for or are your samples the correct ones ?
Thanks in advance,
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,
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.