WordPress multiple tax_query with multiple terms not working - wordpress

I am tired to get posts using this multiple tax_query but not working at all. Can anyone fix this problem please? If anyone can fix the problem, I will grateful to him/her forever.
This query get nothing. My query is exactly look like -
$args = array(
'posts_per_page' => 18,
'post_type' => 'post',
'post_status' => 'publish',
'tax_query' => array (
'relation' => 'AND',
array (
'taxonomy' => 'taxonomy-1',
'terms' => array( 2024 ),
'field' => 'term_id',
),
array (
'taxonomy' => 'taxonomy-2',
'terms' => array( 2025, 217 ),
'field' => 'term_id',
),
),
);
$posts = get_posts( $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

Wordpress: Multiple taxonomy terms in WP_Query

I am trying to get multiple taxonomy terms in WP_Query. Here is my code:
$category = get_field('portfolio_category'); //array of IDs like 14, 15, 16
<?php
$the_query = new WP_Query(array(
'post_type' => 'projects',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'projectCategories',
'field' => 'term_id',
'terms' => array( implode(', ', $category ) ),
'operator' => 'AND'
)
),
));
?>
The problem is in the current code I can only query the very first term.
For example I can only get projects from cat ID=14.
What am I doing wrong here? How can I query posts from multiple terms?
Thank you.
Since $category is already an array... change your terms value to just $category. so your full query would be:
$the_query = new WP_Query(array(
'post_type' => 'projects',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'projectCategories',
'field' => 'term_id',
'terms' => $category,
'operator' => 'AND'
)
),
));

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 );
?>

one post per term taxonomx

I need some help, again.
I use this query:
$tax = get_the_terms($id, 'coupon_category');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( array(
'post_type' => APP_POST_TYPE,
'post_status' => 'publish',
'posts_per_page' => 4,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'coupon_category', // Taxonomy
'field' => 'slug',
'terms' => $tax[0]->slug, // Your category slug
),
array(
'taxonomy' => APP_TAX_STORE,
'field' => 'slug',
'terms' => $term->slug,
'operator' => 'NOT IN',
),
),
) );
I know need a solution to only show one post from each Taxonomy term APP_TAX_STORE. Tried some solutions from this userfull forum but non worked for me.

Wordpress get posts not in custom taxonomy term

The following code is supposed to get posts that do not have specific terms in a custom taxonomy. At the moment it still gets them. Is something missing.
$args = array(
'numberposts' => '3',
'post__not_in' => $post_not_in,
'tax_query' => array(
'taxonomy' => 'topic',
'terms' => 9,
'field' => 'id',
'operator' => 'NOT IN'
)
);
$extras = get_posts($args);
Important Note: tax_query takes an array of tax query arguments arrays (it takes an array of arrays)
— Wordpress Codex on Taxonomy Parameters
Have you tried?
$args = array(
'numberposts' => '3',
'post__not_in' => $post_not_in,
'tax_query' => array(
array(
'taxonomy' => 'topic',
'terms' => 9,
'field' => 'id',
'operator' => 'NOT IN'
)
)
);
$extras = get_posts($args);

Resources