I have this query based on WordPress docs, but no result. If I remove the tax_query it gives all publish posts, but when I add it, it displays no result.
I have checked the database and I'm sure there is post attached to the wp_term_taxonomy so I'm not sure why it doesn't display results.
What I am doing here, I get all custom taxonomy type then loop through it to get all posts related to it.
$event_terms = get_terms(
'event_type',
array(
'orderby'=>'name',
'hide_empty'=>true
)
);
if(!empty($event_terms) && !is_wp_error($event_terms)){
foreach($event_terms as $event_term){
// code for each event term
$args = array(
'status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'event_type',
'field' => 'slug',
'terms' => $event_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$event_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo ''.get_the_title().'<br>';
endwhile;
}
// this displays all taxonomy terms
echo $event_term->name."-".$event_term->term_taxonomy_id."<br>";
}
}
Please change your text query like this and try:
$the_query = new WP_Query( array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'event_type',
'field' => 'slug',
'terms' => $event_term->slug
)
)
) );
$count = $the_query->found_posts;
I hope this is helps you.
Related
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),
)
)
);
I have created some taxonomies and post_types trough custom post type UI plugin.
I pass taxonomy_id with a form to a page and I receive it correctly [var_dump($_POST)] shows me number example 30. I want to show posts from that custom post type category: I tried the code below but it returns nothing.
$args = [
'tax_query' => array(
array(
'taxonomy' => 'school_type',
'field' => 'term_id',
'terms' => array('30','22'),
// 'operator' => 'IN'
),
'post_type' => 'school',
)
];
if($q->have_posts()):
while($q->have_posts()){
$q->the_post();
echo the_title();
}
else:
echo 'nothing';
endif;
Can anyone help me?
You should have a $arg variable like this and then use the WP_Query() object to get your posts.
$args = array(
'post_type' => 'school',
'posts_per_page'=>30,
'post_status' => 'publish',
'tax_query' => array(array(
'taxonomy' => 'school_type',
'field' => 'term_id',
'terms' => array('30','22'),
);
$q = new WP_Query($args);
if($q->have_posts()):
while($q->have_posts()){
$q->the_post();
echo the_title();
}
else:
echo 'nothing';
endif;
Put $q = new WP_Query( $args ); before your if line
change your permalink to post type and update permalinks, hope this will help.
You have the post_type in the tax_query array, plus you're targeting $q when it doesn't exist (not that I can see. anyway).
Try something like this :-
$args = array(
'post_type' => 'school',
'tax_query' => array(
array(
'taxonomy' => 'school_type',
'field' => 'term_id',
'terms' => array('30','22'),
),
),
'numberposts' => -1
);
$q = new WP_Query($args);
if($q->have_posts()):
while($q->have_posts()){
$q->the_post();
echo the_title();
}
else:
echo 'nothing';
endif;
I need to add related posts to my custom post type (using current taxonomy term). I know how to display them, I just need to be able to retrieve the currently selected taxonomy terms.
Here's what I'm doing:
$args = array(
'post_type' => 'mycustomposttype',
'posts_per_page' => 3,
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'thetaxonomy',
'terms' => 'theterm',
'field' => 'slug',
)
),
);
I need to dynamically populate "thetaxonomy" and "theterm" based on what's currently selected on the page. Getting the term is easy enough, but I'm struggling to get "thetaxonomy" slug.
Thanks in advance!
FIGURED IT OUT!
In case anyone is looking to accomplish the same thing
$currentTax = get_the_taxonomies($post->ID);
foreach($currentTax as $slug => $tax) {
$currentSlug = $slug;
};
$theTerms = array();
$term_list = wp_get_post_terms($post->ID, $currentSlug, array("fields" => "all"));
foreach($term_list as $term_single) {
array_push($theTerms, $term_single->slug);
}
So the query would look like this...
$args = array(
'post_type' => 'mycustomposttype',
'posts_per_page' => 3,
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => $slug,
'terms' => $theTerms,
'field' => 'slug',
)
),
);
With use of the Advanced Custom Fields plugin I created a select dropdown which contains 6 membership types. All of my 'listings' using this custom field are assigned one of the 6.
I have managed to get my listings to display in order of membership level, however it's not defining by category that your currently in. It's grabbing listings from every other category too.
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'directory_listings',
'meta_key' => 'membership_type',
'orderby' => 'meta_value',
'taxonomy' => 'listing_category'
);
// query
$wp_query = new WP_Query( $args )
?>
<?php if (have_posts()) : ?>
<?php
while( $wp_query->have_posts() ) {
the_post();
ldl_get_template_part('listing', 'compact');
ldl_get_featured_posts();
}
?>
<?php else : ?>
<?php endif; ?>
Also, I am using the plugin: https://wordpress.org/plugins/ldd-directory-lite/
WP_Query does not have a taxonomy parameter, you should use tax_query instead. More info in the Codex.
'tax_query' => array(
array(
'taxonomy' => 'listing_category',
'field' => 'slug',
'terms' => 'my-listing-category',
),
),
To grab the current taxonomy term dynamically (assuming you're on a listing_category taxonomy page):
'tax_query' => array(
array(
'taxonomy' => 'listing_category',
'field' => 'term_id',
'terms' => get_queried_object_id(),
),
),
I want archives.php to be able to show multiple custom taxonomies at once.
Right now this works if I write either
domain.com/?taxonomy=red&taxonomy2=circle
or
domain.com/?taxonomy=red+blue
But how do I make my list of taxonomies link correctly to this? Right now I'm only able to get domain.com/?taxonomy=red and not add anything else to the URL.
The sidebar on archives.php contains the following code:
<?php
//First Query for Posts matching term1
$custom_terms = get_terms('taxonomy');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'taxonomy',
'tax_query' => array(
array(
'taxonomy' => 'taxonomy',
'field' => 'slug',
'terms' => $custom_term->slug,
'orderby' => 'title'
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<li>'.$custom_term- >name.'';
echo '</li>';
}
}
?>