Home › Forums › General Issues › Query for Taxonomy with ACF User Field
Hello,
I’m hoping to get my hands on a custom query that will show only WooCommerce products in a category that are assigned to a User using the ACF User field.
For instance, there is a Product category named “Category X,” and in the User custom field that appears on its Product category settings page, I selected “User X,” creating a relationship between the “Category X” and “User X.”
Now I need a query to show only the products (in “Category X”) for “User X” when they are logged in. This query needs to work for any particular user (I will be assigning different product categories to different users).
I’m trying to use Dynamic.ooo to set this query, but unfortunately the options don’t allow it, so it must be custom.
Preferred Query ID: user_category
ACF Settings:
Field Type: User
Field name: category_user
Filter By Role: Customer
Return Format: User Array (or whatever works best)
Show this field group if: Taxonomy is equal to Category (product_cat)
I tried the below but it is not working:
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'taxonomy' => 'product_cat',
//Tries to show products from categories where current user ID is the same as the category_user field
'meta_query' => array(
array(
'key' => 'category_user',
'value' => $current_user->ID,
'compare' => 'LIKE'
)
)
);
You cannot do this with only a query on the posts (products) because the user field is not associated with the posts, it is associated with a term (category).
What you will need to do is first to use get_terms() with a meta_query to get a list of terms that are associated with the user. Using this you then query the posts using a taxonomy_query of the terms found in the first step.
Thank you for the reply. I appreciate it. Last night before your response, I realized what you said might be the case and I have to admit I spent hours trying to figure it out to no avail. What would a query like the one you’ve described look like?
The following may not have all the arguments you need.
$args = array(
'taxonomy' => 'TAXONOMY NAME'
'meta_query' => array(
array(
'key' => 'FIELD NAME'
'value' => '"'.$current_user->ID.'"',
'compare' => 'LIKE'
)
),
'fields' => 'ids' // return just a list of term IDs
);
$terms = get_terms($args);
$args = array(
'post_type' => 'product'
'taxonomy_query' => array(
array(
'taxonomy' => 'TAXONOMY NAME',
'terms' => $terms
)
)
);
$query = new WP_Query($args);
When I plug in the correct values for taxonomy and key, it simply returns all products. Also when I try and just echo the $terms alone to see what they’re returning, it just says “Array.”
Here is the code:
$args = array(
'taxonomy' => 'product_cat',
'meta_query' => array(
array(
'key' => 'category_user',
'value' => '"'.$current_user->ID.'"',
'compare' => 'LIKE',
)
),
'fields' => 'ids' // return just a list of term IDs
);
$terms = get_terms( $args );
$args = array(
'post_type' => 'product',
'taxonomy_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => $terms,
)
)
);
$query = new WP_Query( $args );
$terms should be returning an array. To see the term IDs returned use print_r() instead of echo.
Thank you John. Below is the complete solutionmodel for anyone looking for this setup and/or solution in the future:
'value' => '"'.$current_user->ID.'"',
had to be 'value' => $current_user->ID,
or 'value' => get_current_user_id(),
'taxonomy_query' => array(...
had to be 'tax_query' => array(...
'field' => 'term_id',
had to be added to the tax_query arguments
Complete solution:
$args = array(
'taxonomy' => 'product_cat',
'meta_query' => array(
array(
'key' => 'category_user',
'value' => $current_user->ID,
'compare' => 'LIKE',
)
),
'fields' => 'ids' // return just a list of term IDs
);
$terms = get_terms( $args );
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $terms
)
)
);
$query = new WP_Query( $args );
Forum won’t let me edit my last post for some reason. In fact, it disappeared. Here is the complete solution that’s working:
$args = array(
'taxonomy' => 'product_cat',
'meta_query' => array(
array(
'key' => 'category_user',
'value' => $current_user->ID,
'compare' => 'LIKE',
)
),
'fields' => 'ids' // return just a list of term IDs
);
$terms = get_terms( $args );
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $terms
)
)
);
$query = new WP_Query( $args );
You must be logged in to reply to this topic.
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.