WP_Query Multiple Categories - wordpress

I've My categories Structure like this :
Music
-Genre
-Pop
-Rock
-Role
-Vocalist
-Guitarist
So Now I want to filter posts in such a way that a post should belong to ( either Pop or Rock) and (Vocalist)
So an or between genres and an and with the role
So speaking in wordpress terms in need multiple category_in or category_and which has category_in
Tricky right?
Any solution?

Try this and see if it works.
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'vocalist' ),
),
array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'pop' ),
),
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'rock' ),
),
),
),
);
$query = new WP_Query( $args );

Related

How to query Standard and Video Post Format in WordPress?

I am trying to query posts according to their post format. Currently, I have the video, gallery, and standard post formats on my site. It's quite easy to query these post formats individually. However, I have a combination of tax queries of 10 posts that are standard and video post format, my query fails.
Here's the code I have right now:
$args = array(
'post_status' => 'publish',
'orderby' => 'date',
'posts_per_page' => 10,
'relation' => 'OR',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'operator' => 'IN',
'terms' => array( 'post-format-video' )
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-video', 'post-format-gallery' ),
'operator' => 'NOT IN'
)
)
);
$query = new WP_Query( $args );
I think, you should put the 'relation' => 'OR', into the 'tax_query'-Array.
You are adding 'relation' => 'OR' in the wrong place. it should be inside the tax_query array. check the below code.
$args = array(
'post_status' => 'publish',
'orderby' => 'date',
'posts_per_page' => 10,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'operator' => 'IN',
'terms' => array( 'post-format-video' )
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-video', 'post-format-gallery' ),
'operator' => 'NOT IN'
)
)
);
$query = new WP_Query( $args );
USEFUL LINKS
taxonomy-parameters

How do I query posts that don't have both of two tags?

Say I have two tags, red and small. I want to get all posts that are not red and small. I want to include posts that are either red or small, but they can't be both.
The tag__not_in parameter won't work, because it'll exclude posts that are just red or just small.
I also tried the tax query below but then realized it's the same as the above.
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'red',
'operator' => 'NOT IN',
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'small',
'operator' => 'NOT IN',
),
)
I feel like there's got to be a way to do this and I'm just missing something simple. That and I'm getting confused by the logic inversions. Any ideas?
You can try again
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'red',
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'small',
),
)
);
$query_red_small = new WP_Query( $args );
$post_ids = wp_list_pluck( $query_red_small->posts, 'ID' );
$args = array(
'post_type' => 'post',
'post__not_in' => $post_ids
);
$query = new WP_Query( $args );
?>

Filter on Category and Postformat

I have just started in Wordpress Template development, I am getting used to it.
However I have been looking to find the solution here but unable to find the same question.
I would like to filter on Category and Post-Format, i have the following code the Filter on the Post Format works however not on the Category.
$videos = new WP_Query( array(
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-video'
),
'category_name=instructie-videos',
'posts_per_page=6',
'operator' => 'IN'
)
)
) );
$videos = new WP_Query( array(
'post_type' => 'your-post-type',
'posts_per_page' => 6,
'category_name' => 'instructie-videos',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-video' ),
'operator' => 'IN'
)
)
) );
maybe it's syntax mistake near category_name. Try above code
you should try as below:
$myposts = new WP_Query( array(
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-aside',
'post-format-audio',
'post-format-chat',
'post-format-gallery',
'post-format-image',
'post-format-link',
'post-format-quote',
'post-format-status',
'post-format-video'
),
'operator' => 'NOT IN'
)
)
) );

Wordpress taxonomy query with both and , or condition

i have product post type and i have product_cat is the taxonomy . In product_cat i have red, hard, soft, pen entires .
So i have to get the products coming under red and it coming hard or soft
How can i get this ?
For a product that come under both red and hard and soft i can use the following query
$args = array(
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'red'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'hard'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'soft'
)
),
'post_type' => 'product',
'orderby' => 'title',
);
But what i need is red is must and either in soft or hard .
ie (red && (soft||hard ))
Please help .
you can try this way:
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'red' ),
),
array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'hard' ),
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'soft' ),
),
),
),
Is untested, but it should work!
If not, here some useful links:
https://codex.wordpress.org/Class_Reference/WP_Query
https://10up.com/blog/2013/wordpress-mixed-relationship-taxonomy-queries/

how do i combine post meta data and taxonomy in a WP_query $args array Worpresss

I'm trying to figure out how to get the following code to work as one wp_query . i'm trying to filter the loop on the meta values and the taxonomy values:
Both of these sections work independently but i'm looking to tie these into one query..?
I have tried various combinations but am getting no where....any ideas of the best approach to this issue...?
$args = array(
array(
'post_type' => 'job_listing',
'meta_key' => 'geo_short_address',
'meta_value' => $area
),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'job_type',
'field' => 'term_id',
'terms' => $jobtype
),
array(
'taxonomy' => 'job_cat',
'field' => 'slug',
'terms' => $jobcat
)
)
);
It looks as if your argument array is one level too deep. Try this:
$args = array(
'post_type' => 'job_listing',
'meta_key' => 'geo_short_address',
'meta_value' => $area,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'job_type',
'field' => 'term_id',
'terms' => $jobtype
),
array(
'taxonomy' => 'job_cat',
'field' => 'slug',
'terms' => $jobcat
)
)
);
If that doesn't work, try this:
$args = array(
'post_type' => 'job_listing',
'meta_key' => 'geo_short_address',
'meta_query' => array(
array(
'key' => 'geo_short_address',
'value' => $area
)
),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'job_type',
'field' => 'term_id',
'terms' => $jobtype
),
array(
'taxonomy' => 'job_cat',
'field' => 'slug',
'terms' => $jobcat
)
)
);

Resources