Get all posts that have NO terms with WP_Query - wordpress

I am writing a wordpress loop, and I want to get all the posts that have NO terms assigned to them. Is there an easy way to do it? Or do I really have to get all the term ID's and do an tax query like this :
// Get all the term id's
$terms = array();
$terms = getAllTheTerms();
// Create an arguments which get all the posts that do not have a term with any
// of the id's.
$args = array(
'post_type' => 'post',
'tax_query' =>
array(
'taxonomy' => 'actor',
'field' => 'id',
'terms' => $terms,
'operator' => 'NOT IN'
)
);
$query = new WP_Query( $args );
This seems like a stupid query because database wise it would be very easy to get all the posts without a query.

$terms = get_terms( $taxonomy, array('fields'=>'ids')); /* GET ALL TERMS FROM A TOXNOMY */
$args = array(
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => $terms,
'operator' => 'NOT IN' /* DO THE MAGIC - Get all post that no in the taxonomy terms */
)
)
);
$the_query = new WP_Query( $args );

The question was: "I am writing a wordpress loop, and I want to get all the posts that have NO terms assigned to them. Is there an easy way to do it?"
Here is the answer: the following code replaces the code of the original poster and demonstrates the best solution.
$args = [
'post_type' => 'post',
'tax_query' => [
[
'taxonomy' => 'actor',
'operator' => 'NOT EXISTS',
],
],
];
$query = new WP_Query($args);

You may try this
$args = array(
'post_type' => 'post',
'tax_query' =>
array(
'taxonomy' => 'actor',
'field' => 'slug',
'terms' => '',
)
);
$query = new WP_Query( $args );

Related

In WordPress, get all posts having keyword in Title, Tag or Category?

My question is similar to this one, but it's now 7 years old, so I thought I'd ask again.
I'm using the get_posts() function but it seems that whatever is passed as the 's' parameter is only matched against the post title. For example, this code only returns posts containing 'sunflower' in the title, not posts containing 'sunflower' in one of their tags
$args = array( 'numberposts' => 99, 's' => 'sunflower');
$postslist = get_posts( $args );
I'm just starting out with WP development, so maybe I'm overlooking something or using the wrong function... Any pointers will be greatly appreciated!
To additionally get posts with 'sunflower' as a tag you need to add taxonomy parameters like so:
$args = [
'numberposts' => 99,
's' => 'sunflower',
'tax_query' => [
[
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'sunflower'
]
]
];
$postslist = get_posts( $args );
Official docs: https://developer.wordpress.org/reference/functions/get_posts/
IMHO this is much more complicated than it should be, but I managed to get what I want with the following code:
$searchTerm = trim($_REQUEST['search']);
// Get all slugs that are a partial match in tags
$matching_terms_tags = get_terms( array( 'taxonomy' => 'post_tag', 'fields' => 'slugs', 'name__like' => $searchTerm ) );
// Get all slugs that are a partial match in categories
$matching_terms_categories = get_terms( array( 'taxonomy' => 'category', 'fields' => 'slugs', 'name__like' => $searchTerm ) );
// Build taxonomy query
$argsTax = array('numberposts' => 999, 'posts_per_page' => -1, 'nopaging' => true);
$argsTax['tax_query'] = array
(
array
(
'relation' => 'OR',
array ('taxonomy' => 'category', 'field' => 'slug', 'terms' => $matching_terms_categories,'operator' => 'IN',),
array ('taxonomy' => 'post_tag', 'field' => 'slug', 'terms' => $matching_terms_tags, 'operator' => 'IN', ),
),
);
// Get all posts with matching tags and/or matching categories
$postsTax = get_posts($argsTax);
// Also get all posts matching the term, using the regular WP argument 's'
$argsTerms = array('numberposts' => 999, 'posts_per_page' => -1, 'nopaging' => true, 's' => $searchTerm);
$postsSearch = get_posts($argsTerms);
// Merge the 2 result sets and remove duplicates
$postsAll = array_merge($postsSearch, $postsTax);
$postAllNoDupes = array_map("unserialize", array_unique(array_map("serialize", $postsAll)));
foreach ($postAllNoDupes as $post)
{
echo get_the_title($post);
}
wp_reset_query();

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

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 Woocommerce products that belong in distinct multiple categories only tax_query

I'm using WP_Query for Woocommerce products in attempt to query products in a particular category. This is the syntax that worked for me -
$args = array(
'posts_per_page' => -1,
'product_cat' => 'category-slug-here',
'post_type' => 'product',
'orderby' => 'title',
);
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '' . get_the_title() . '<br /><br />';
}
wp_reset_postdata();
This returns data, but I want to pass an ID, not a category slug, to filter and I want to find products that exist in multiple categories only.
The argument product_cat is not native to WP_Query (at least that I can find), so I'm assuming this is something custom to Woocommerce. Through their documentation, I haven't been able to find anything that will allow me to filter by category ID, nor use an AND condition for this filtering.
Using cat, the array of tax_query, and category__and have not yielded any results. Essentially, I would like to query all products that exist in both category ID 102, and 115. If I have to use slugs, I'm sure there is a way around getting that info based on the ID I have, but I'd like to avoid 2 queries to filter by multiple categories.
Does anyone know how to accomplish this?
UPDATE: I have learned that separating category slugs by commas in the product_cat argument will produce an "OR" effect, so it will combine distinct products from both, but this is not what I am looking for. So, for example:
'product_cat' => 'category-slug1, category-slug2'
will return products from both categories in total, but I am still searching for a way to find distinct products that ONLY belong to both, or multiple, categories.
Wow, so after hours of banging my head, this is how I was able to solve this -
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'category-slug1'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'category-slug2'
)
),
'post_type' => 'product',
'orderby' => 'title',
);
$the_query = new WP_Query( $args );
This takes advantage of the tax_query argument, including the relation => 'AND' to make sure the product falls under BOTH categories.
Hope this helps someone in the future.
I was also not able to figure out how to pass an ID, rather than a slug (although I'm sure there's a way), but here's the function to retrieve the slug based on an ID:
$terms = get_term($YOURID, 'product_cat');
$theslug = $terms->slug;
From the WordPress codex on WP_Query for Category Parameters:
equivalent of OR
$args = array( 'product_cat' => 'category-slug1,category-slug2' );
equivalent of AND
$args = array( 'product_cat' => 'category-slug1+category-slug2' );
e.g.
$query = new WP_Query( $args );
To query by category_ID this is what worked for me.
// First obtain term id:
//...
$all_categories = get_categories( $args );
$cat_ids = array();
foreach ($all_categories as $cat)
{
array_push($cat_ids, $cat->term_id);
}
//Now use ids from array:
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cat_ids
)
)
);
Inside a 'tax_query' array's array you can specify an 'operator' to be performed on the query. You can achieve what you want using the 'AND' operator.
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'category-slug1', 'category-slug2' )
'operator => 'AND',
),
),
'post_type' => 'product',
'orderby' => 'title',
);
$the_query = new WP_Query( $args );
All of the products selected by this query will match the provided 'terms'. See this link for more info: https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
In case the link ever breaks, here's the relevant info:
operator (string) - Operator to test. Possible values are 'IN', 'NOT IN', 'AND', 'EXISTS' and 'NOT EXISTS'. Default value is 'IN'.

Resources