Wordpress: Multiple taxonomy terms in WP_Query - wordpress

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'
)
),
));

Related

how to call optiontree taxonomy select

I ask for help to solve my problem, I use the option-tree framework for displaying custom posts from specific categories, I have this code:
<?php
global $product;
$category_product1 = ot_get_option( 'homepage-slider-cat1' );
$args = array(
'post_type' => 'product',
'numberposts' => 18,
'post_status' => 'publish',
'category' => $category_product1,
'include_children' => true
);
?>
with this post code specific category does not appear.
Please help me
Have you tried the Tax_query as a query parameter? This way you can query on taxonomies.
'tax_query' => array(
array(
'taxonomy' => 'people',
'field' => 'slug',
'terms' => 'bob',
),
),
https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

WordPress - SQL request with WP_Query, use OR between post_type and taxonomy

My issue is pretty simple, I'm trying to get both portfolio posts (from a plugin) and articles that have the "portfolio" category (made myself).
$args = array(
'post_type' => 'portfolio',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'portfolio',
)
)
);
$query = new WP_Query($args);
It looks like the relationship between post_type and tax_query is an AND, I'd need an OR, how can I do that?
I tried following the official documentation but I'm stuck. (https://codex.wordpress.org/Class_Reference/WP_Query)
Thanks for the help.
Here is an example with a relation 'OR':
$args = array(
'post_type' => 'portfolio',
'meta_query' => array(
'relation' => 'OR', /* <-- here */
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE'
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
$query = new WP_Query( $args );
I haven't found any solution using only one query. (couldn't change the AND to OR).
So I've made two queries instead:
$query1 = new WP_Query(array('post_type' => 'portfolio'));
$query2 = new WP_Query(array(
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'portfolio',
)
)));
$query = merge_WP_queries($query1, $query2);
function merge_WP_queries(WP_Query $query1, WP_Query $query2){
$wp_query_returned = new WP_Query();
$wp_query_returned->posts = array_merge( $query1->posts, $query2->posts );
//populate post_count count for the loop to work correctly
$wp_query_returned->post_count = $query1->post_count + $query2->post_count;
return $wp_query_returned;
}
It works correctly, even though it's not what I'd like to use.

Wordpress - display posts from a specific custom taxonomy and a certain tag

I need a tax_query that assigned to specific custom category and tags.
Below query showing all products but I need the products to a particular category.
Here tag="sale", category="look"
$args = array(
'post_type' => 'product',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'look' ),
),
array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => 'sale',
),
),
);
$query = new WP_Query( $args );
You can simply use this
$new = new WP_Query('category_name=look&tag=sale');
try to use something like this
$args = array(
'posts_per_page' => 5,
'post_type' => array('videos','image','audio'),
'category_name' => 'look',
'tag'=>'sale'
);
$loop = new WP_Query( $args );

WP_query twice on a single taxonomy

I have a simple WP_query on a custom post and works fine:
$args = array(
'post_type' => 'post-type',
'showposts' => 2,
'location' => 'london'
);
but I want it to query two locations so used an array:
$args = array(
'post_type' => 'post-type',
'showposts' => 2,
'location' => array('london','paris')
);
but it only pulls in the last term. Is there another way that I should be doing this?
$args = array(
'post_type' => 'post-type',
'tax_query' => array(
array(
'taxonomy' => 'location',
'field' => 'slug',
'terms' => array('london','paris')
)
)
);
Try this

Wordpress query not filtering by taxonomy when specifying a slug

I have a custom post type called news. Inside that I have two custom taxonomies called press-releases and media-coverage. When I query for multiple posts with this query:
$args = array(
'post_type' => 'news',
'news_type' => 'press-releases',
'posts_per_page' => 15,
'paged' => $json_api->query->page,
);
$query = new WP_Query($args);
It only gets news posts categorized as press-releases like I want. But if I specify a slug that exists in media-coverage it returns that post even though I have the press-releases taxonomy specified. Example:
$args = array(
'name' => $slug,
'post_type' => 'news',
'news_type' => 'press-releases',
'posts_per_page' => 1,
);
You may try this
$args = array(
'post_type' => 'news',
'name' => '$slug,
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'news_type',
'field' => 'slug',
'terms' => 'press-releases'
)
)
);

Resources