WP_Query category parameter is not working - php

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.

Related

Wordpress filter by taxonomy ACF field

I have a problem searching for posts for which I have created a special field in taxonomy.
$args = array(
'posts_per_page' => '20',
'paged' => $paged,
'post_type' => 'cars',
'order' => 'DESC',
);
Taxonomy name: localization
With my cars post type I have a taxonomy relation, where there is a field I created called "city".
How can I filter posts from "cars" post type by this custom field in taxonomy in wp_query?
I tried to write such tax_query, but I keep doing something wrong. Can you give me an example where someone filters it in a similar way by custom field?
Your query would look something like:
$args = array(
'post_type' => 'cars',
'post_status' => 'publish',
'posts_per_page' => 20,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'localization',
'field' => 'slug',
'terms' => array( 'tax1', 'tax2' )
)
)
);
$query = new WP_Query( $args );

Impossible to filter custom post in query using tax_query

First time than I use Timber and I'm looking for help to find a way to filter some custom_post with a taxonomy.
In this file single-news.php, I try to display in my custom posts (news) controller a link with a an acf taxonomy field named 'categorie_dexpertise'
When I get it I use it in tax_query ...
I try to filer 'collaborateur' post type with term_id 'categorie_dexpertise' in 'expertise-collaborateur' taxonomy
But it doesn't work!
Single-news.php
<?php
$context = Timber::get_context();
$post = Timber::query_post();
$context['post'] = $post;
$context['term'] = new Timber\Term('category-news', 'news');
$expertise_team = get_field('categorie_dexpertise');
$context['expertise_team'] = $expertise_team;
$args = array(
'post_type' => 'collaborateur',
'post_status' => 'publish',
'order' => 'DESC',
'tax_query' => array(
'taxonomy' => 'expertise-collaborateur',
'field' => 'term_id',
'terms' => array(11),
),
);
$context['teamfilter'] = Timber::get_posts($args);
Timber::render('single-news.twig', $context);
Is there a better way or an alternative way to filter my custom-posts?
Thank for your help
:) :)
The tax_query parameter should always be an array of arrays. Try to use this code instead:
<?php
$args = array(
'post_type' => 'collaborateur',
'post_status' => 'publish',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'expertise-collaborateur',
'field' => 'term_id',
'terms' => array(11),
)
)
);

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.

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

Showing posts using custom taxonomy name - wordpress

I have the following line of code:
$args = array( 'category_name' => the_slug(), 'post_type' => 'Feature', 'posts_per_page' => 2, 'orderby'=> rand );
Which works fine in showing two posts from the category, as long as the category name is the same as the slug of the current page. I need to change category name to my custom taxonomy, this is called 'featuring'. I've tried changing category_name to featuring_name but it just showed all posts instead of only ones from that taxonomy.
Ended up with the following:
$args = array(
'showposts' => -0,
'post_type' => 'feature',
'orderby' => rand,
'tax_query' => array(
array(
'taxonomy' => 'featuring',
'field' => 'slug',
'terms' => the_slug()
)
)
);

Categories