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

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

Related

How i can compare value in wordpress custom post type loop using taxonomy as key

I am trying to search posts by comparing the value that i am getting from URL with taxonomy of the post. But i couldn't get it right. Here is my code.
$vendita = $_GET['vendita'];
$the_query = new WP_Query(
array(
'post_type' => 'custom_post_type',
'taxonomy' => 'taxonomy_name',
// 'paged' => $paged,
'meta_query' => array(
'key' => 'taxonomy',
'value' => $vendita,
'compare' => 'LIKE'
)
)
);
Thanks in advance.
First, if you are going to take parameters, you must always sanitize them, like with $vendita = sanitize_text_field($_GET['vendita']);.
This is not tested, but I think what you are looking for is this WP_Query
$the_query = new WP_Query(
array(
'post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy_slug',
'field' => 'slug',
'terms' => $vendita,
),
),
)
);
Note the 'field' can be the term_id or the name instead of the slug, I do not know what $vendita is exactly.
Note tax_query is a nested array, it must be that way.
Note: You can replace array() with the shorter [].
Note: From docs: If you use the_post() with your query, you need to run wp_reset_postdata() afterwards to have template tags use the main query’s current post again.

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

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

Wordpress taxonomy terms

So i'm having problem understanding, how can i set correct terms for my query. Right now my query is following:
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'order' => 'DESC',
'orderby' => 'date',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $taxChild-> term_id,
),
)
);
Currently query is looping trough category taxonomy and returns every post that is queried via url. Example: http://website.com/category/{category-name}. Everything works correctly, except when i post new blog post then it shows latest category with the posts. Query queries all the categories as a sectors not post by post like it should be. Then i discovered that i can control the tax query with terms argument, but i'm not sure how can i set it up that i queries only most recent posts not categories.
Full source code for category.php https://pastebin.com/1Rvk1b7X
Example: https://imgur.com/a/abbI4

WP_Query category parameter is not working

I am creating shortcode with attributes and all attributes working except event_category.
When i add "event_category" attribute it is not giving result as per added category.
Here are my shortcode attributes
// Shortcode Default Array
$default_shortcode_attrs = array(
'type' => 'upcoming',
'search' => 'true',
'event_category' => '',
'events_limit' => '-1',
);
extract(shortcode_atts($default_shortcode_attrs, $attr));
Following are query parameters
$args = array(
'posts_per_page' => -1,
'post_type' => 'event_listing',
'post_status' => 'publish',
'event_listing_category'=> $event_category,
);
$query = new WP_Query($args);
"event_listing_category" is name of custom taxonomy. Please guide me why this query is not fetching the events according to their category.
Any help will be appreciated.
Thanks
Use tax_query instead as below:
I'm assuming you have only one category slug provided in $event_category. If you have more than one category slug in that variable then try converting it to array and replace the whole array($event_category) with the array.
$args = array(
'posts_per_page' => -1,
'post_type' => 'event_listing',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'event_listing_category',
'field' => 'slug',
'terms' => array( $event_category)
)
)
);
$query = new WP_Query($args);
See http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters on how to query for custom taxonomies within your query.
The WP_Query function doesn't understand 'event_listing_category' as an argument, you'll have to tell wordpress that it's a custom taxonomy you want.

I am having trouble excluding a custom taxonomy from an archive page for a custom post type in wordpress

I have written a piece of code to build a paginated archive page for a custom post type. I cannot exclude a certain custom taxonomy. can anyone help, please?
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=30&post_type=my_custom_post_type'.'&paged='.$paged);
You have to include the tax_query parameter in query array like here:
$query_args = array(
'showposts' => 30,
'paged' => $paged,
'post_type' => 'my_custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom-taxonomy-name',
'field' => 'slug',
'terms' => 'slug-name',
'operator' => 'NOT IN')
),
)
);
$wp_query->query($query_args);
Please write your custom taxonomy name at the place of 'custom-taxonomy-name' and taxonomy slug name at the place of 'slug-name'

Categories