I am running a wordpress loop that grabs posts from two post types.
I need the loop to grab posts with certain categories set. As it's two separate post types, they both have a category taxonomy. When a user selects one of the categories on the front end, I want the loop to grab all posts from one post type with that category under it's respective taxonomy, as well as all posts from the other post type with that category under its taxonomy.
To simplify the description of what I'm trying to do:
- User clicks on category 'Hair and Skin Care' on front end
- Loop grabs all posts under 'post' with category 'hair-skin-care' from 'category_name'
- Loop grabs all posts under 'quizzes' with category 'hair-skin-care' from 'quiz_category'
- Displays all posts that were found
Getting the loop to grab from both post types has not been an issue, but I'm stuck on trying to get the loop to grab only posts with specific categories. I'm trying out a 'tax_query' but the loops seems to just be ignoring it entirely, as if I didn't put the tax_query at all.
Running either 'category_name' or 'quiz_category' on their own outside of the tax_query works just fine. But within the tax_query, they're both completely ignored.
$cat = $_GET['cat'];
$cat = sanitize_text_field($cat);
$args = array(
'post_type' => array('post', 'quizzes'),
'posts_per_page' => -1,
'category__not_in' => array( 9, 10 ),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category_name',
'field' => 'slug',
'terms' => $cat
),
array(
'taxonomy' => 'quiz_category',
'field' => 'slug',
'terms' => $cat
),
)
);