'I'm trying to make a list of all the posts classified under one term for a custom taxonomy.
I'm trying to make a list of all the posts classified under one term for a custom taxonomy.
I have created a custom post type called "testimonial" with a custom taxonomy of "testimonial categories". with in testimonial categories I have created the terms "colleagues" and "clients". I am trying to create too archive pages. One that will list all of the posts under colleagues and the other list all posts under clients. I have created the archive pages taxonomy-testimonial_categories-clients.php and taxonomy-testimonial_categories-colleagues.php. And can creat a list of all post under the cpt testimonials but can't filter it by the terms Colleagues or clients.
After research on wordpress.org I believe that using tax_query with new WP_Query is the way to go. Here is the code I'm working with now.
<?php
$args = array(
'post_type' => 'testimonial',
'tax_query' => array(
array(
'taxonomy' => 'testimonial_categories',
'field' => 'slug',
'terms' => 'colleagues'
)
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<span class="frame small alignleft">
<?php the_post_thumbnail(thumbnail); ?>
<span>
<div class="test-content">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
Solution 1:[1]
Although its way too late here is the only solution that worked for me,
$my_taxonomy_name = "your taxo. name";
$my_term_name = "your term name";
$my_term_id = -1;
$terms = get_terms($my_taxonomy_name);
if(count($terms)>0){
foreach ($terms as $term) {
if($term->name == $my_term_name){
$term_id=$term->term_id;
continue;
}
}
$my_posts = array();
$postids_in_term = get_objects_in_term($term_id,'ad_setting');
foreach($postids_in_term as $post_id){
array_push($my_posts,get_post($post_id));
}
var_dump($my_posts);
}
In no way this is elegant, but hey, it works!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | nthapa |
