WP Query returning results even when $args are not met? - php

In the below WP Query, there seems to be a slightly odd behaviour. When the criteria is met, the correct products are shown, however, if not products match the criteria, other products are shown, rather than having no results? I'm unsure why this might be, but I would assume it is something to do with the tax_query section. In short, what it is trying to retrieve is products from the current tag, that are NOT tagged as featured products (I have another query that targets featured products so this is to avoid duplication).
How can I ensure no results are shown if the $args are not met?
Thanks.
//Get current Tag
$currentTag = get_term_by('slug', $wp_query->get_queried_object()->slug, 'product_tag');
$args = array(
'post_type' => 'product',
'status' => 'publish',
'fields' => 'ids', // Only return IDs
'orderby' => 'menu_order',
'order' => 'ASC',
'limit' => -1,
'tax_query' => array(
'relation' => 'AND',
// Only items from current product_cat
array(
'taxonomy' => 'product_tag',
'field' => 'term_id',
'terms' => $currentTag->term_id,
'operator' => 'IN',
),
// AND are not featured
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'NOT IN',
),
),
);
$query = new WP_Query( $args );
EDIT:
After some further research, it seems the problem is coming form another query further down the page. First, I perform 2 WP Queries as per the above, to retrieve different product types, and then merge these into a single array of ID's. Then, I perform the below wc_get_products() query to actually retrieve the product data. As you can see, the include arg states that only the ID's previously queried should be included, but it seems to ignore this when there are no matching products and instead revert to honour the per_page arg. Any way to avoid this and ensure they are excluded? Is there a way to 'exclude' anything that isn't in the $mergedQueries?
$productsData = wc_get_products(array(
'include' => $mergedQueries,
'paginate' => true,
'page' => $paged,
'limit' => 3,
'orderby' => 'post__in',
'return' => 'objects',
));

Related

What is taxonomy 'product_visibility' used for?

I hooked in the WP_query params of a pre-made widget of my theme that's returning a product strip of the best selling products in my eshop.
The params array looks like this:
array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'posts_per_page' => 6,
'meta_query' => array(),
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => array(14, 16),
'operator' => 'NOT IN',
),
),
'meta_key' => 'total_sales',
)
The reason I hooked there is because I need to alter the query params and use the widget to show some products based on some other criteria.
However, I'm a bit confused with this part of the params:
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => array(14, 16),
'operator' => 'NOT IN',
),
),
So, what's this product_visibility taxonomy? I've never noticed it before. Is it stock WC functionality or is it from my theme? I looked through phpMyAdmin in wp_terms table and term IDs 14 and 16 are exclude-from-catalog and outofstock respectively, but I never set such taxonomies. Judging by their IDs (being small ones) I suspect they are stock functionality, but are they?
Can anyone point me to the right resource to read about this feature online?
TIA.

Wordpress sort by number custom fields gives different results

I have a query which gives me custom post type posts which are sorted by category and a custom fields which has a number that indicates the amount of votes for the post. The problem is, if 2 posts have 3 votes, sometimes when you refresh, it changes their position. Here is the code:
$args = array(
'numberposts' => 10,
'post_type' => 'nominees',
'meta_query' => array(
array(
'key' => 'category',
'value' => get_the_ID(),
'compare' => '=',
)
),
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => 'count_nominees'
);
I tried adding order on the category meta query, but nothing changes, I still sometimes get different results when refreshing, if the posts have same amount of votes. What is the right way to add second order, by ID or something?
Thanks.
As mentioned in the comments, I think this might help, but you might be able to extend it to be a little more specific in the search query for your use case.
$args = array(
'numberposts' => 10,
'post_type' => 'nominees',
'meta_query' => array(
array(
'key' => 'category',
'value' => get_the_ID(),
'compare' => '='
)
),
'meta_key' => 'count_nominees',
'orderby' => array(
'count_nominees' => 'DESC',
'ID' => 'DESC'
)
);
That should get 10 posts in the nominees post type, only if they're part of category xyz, and have post meta of count_nominees and order by count_nominees first in descending order and then by the post ID in descending order.
You can use the WP_Query documentation on the WordPress codex for more information about complex queries using post meta data.

Show count of Custom Taxonomy only for a specific Custom Post Type

I want to display the count of a Custom Taxonomy based on a specific Custom Post Type. At the moment, I'm using get_terms to list all terms of the Taxonomy.
The Taxonomy is used by more than one Post Type. So the counter shows all the usage of the Taxonomy for every Post Type.
Is there a way to limit the count on a single Post Type?
Here is my actual code:
get_terms(array(
'taxonomy' => 'tax',
'hide_empty' => true,
'orderby' => 'count',
'order' => 'DESC',
'number' => '10',
));
Inside the foreach, I'm using term->count to show the usage counter.
I wouldn't recommend using get_terms because this returns all terms of a taxonomy and not all terms associated with posts.
The alternative solution is to use get_posts, read more here https://developer.wordpress.org/reference/functions/get_posts/
$my_posts = get_posts(array(
'post_type' => 'post', //post type
'numberposts' => -1,
'tax_query' => array(
array(
'taxonomy' => 'tax', //taxonomy name
'field' => 'id', //field to get
'terms' => 1, //term id
)
)
));
Then you can count the number of posts returned:
$count = count($my_posts);
I think following link are more helpful to better understanding.
Link
And this is code serve for you.
$args = array(
'post_type' => 'Your_custom_post_type',//Your custom post type here.
'tax_query' => array(
array(
'taxonomy' => 'Your_taxonomy',//Your taxonomy is here.
'field' => 'slug',
)
)
);
Now we print_r the $args for the better understanding exactly what we get.
_e('<pre>');
print_r($args);
_e('</pre>');
Just get your query
$your_query = new WP_Query( $args );

Excluding Specific Terms from WP_Query

I would like to create a foreach loop for taxonomy terms which is for custom post type.
More specifically I want a loop that queries all the products categories, but not the category "special-offers" and not categories subcategories. Bonus would be if, product has no category query them too and order all of them in ASC order (Not like sort products and categories separately. All of them must be sorted at the same time).
So what should I do with my code to make it work as needed?
Current code:
<?php
$args = array(
'post_type' => 'products',
'showposts' => -1,
'post_status' => 'publish',
'parent' => 0,
'hide_empty' => true,
'tax_query' => array(
'taxonomy' => 'categories',
'field' => 'slug',
'terms' => array( 'special-offers', 'other-terms' ),
'operator' => 'NOT IN',
),
);
$terms = get_terms('categories', $args );
foreach ( $terms as $term ) :
echo '<h2>' . $term->name . '</h2>';
endforeach;
?>
Your Tax Query is should be looking within another array.
'tax_query' => array(
array(
'taxonomy' => 'categories',
'field' => 'slug',
'terms' => array( 'special-offers', 'other-terms' ),
'operator' => 'NOT IN',
)
),
Rest of it seems okay.
Check out the WP_Codex on this
Final solution was to add exclude and term id to the taxonomy arguments. Since it is for taxonomy and it uses foreach loop.
$args = array(
'parent' => 0,
'hide_empty' => true,
'exclude' => 13,
);
And answer for how to output custom post type posts with no taxonomy can be found here: http://www.codeforest.net/wordpress-tip-show-posts-no-category-term
Thanks to CBroe and ste for their time.
Just incase someone runs into this in the year 2022 haha
$query = new WP_Query( array( 'category__not_in' => array( 2, 6 ) ) );

wordpress the_post method in custom category page not showing top viewed post

I have Most viewed posts(in Customer Category), in home page I have added this widget and it works perfect, in detail page also it works perfect but in category page the same widget behaves strangely
$r = new WP_Query( array( 'tax_query' =>
array(
'relation' => 'OR',
array(
'taxonomy' => 'custcategory',
'field' => 'term_id',
'terms' => array(10)),
),
'category__in'=>array(10),
'post_type'=> $post_type ,
'posts_per_page' => $number,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC' ) );
if ($r->have_posts()) :
// Enters this block in home page and detail page
while ( $r->have_posts() ) : $r->the_post();
else:
// Enters this block in category page
Anyone know why this strange behavior is?
Maybe you have some filter that is affecting categories page, add this to your query args:
'suppress_filters' => true
You can also try adding wp_reset_query() after your main query, but this should not be necessary because you are declaring a new WP_Query.
I just realize that your query args are wrong. You have set a relation, but you only have one taxonomy array. Also you don't need the 'category__in'=>array(10) argument, it is only for the default taxonomy "category" and you want only posts from your custom taxonomy "custcategory", isn't it?
Your query should be:
$r = new WP_Query( array(
'tax_query' => array(
array(
'taxonomy' => 'custcategory',
'field' => 'term_id',
'terms' => array(10)
),
),
'post_type'=> $post_type ,
'posts_per_page' => $number,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC'
));

Categories