Search code examples
wordpresscategoriestaxonomy

How to Display Terms for all posts in Current Archive or Query in wordpress


I have custom post types with select option.In select option I got all the taxonomy categories and on change of select it displayes all posts of post type but when I go to single page it displayes only this current post not all from this category. I tried this code $term = $wp_query->queried_object; but it displayes only the current post of this category not all.

How can I get all posts from the current taxonomy category.


Solution

  • Seems I got the solution.Here is my final code

    $terms = wp_get_post_terms( $post->ID, 'course_type' );
    if($terms){
    // post has course_type terms attached
    $course_terms = array();
    foreach ($terms as $term){
     $course_terms[] = $term->slug;
    }
    $original_query = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query( array(
    'post_type' => 'courses',
    'tax_query' => array(
     array(
    'taxonomy' => 'course_type',
    'field' => 'slug',
    'terms' => $course_terms, //the taxonomy terms I'd like to dynamically query
    'posts_per_page' => '-1'
      ),
    ),
    'orderby' => 'title',
    'order' => 'ASC'
    ) );
    if ( have_posts() ): ?>
    <ul>
     <?php while (have_posts() ) : the_post(); ?>
      <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?     php the_title(); ?></a></li>
      <?php endwhile; ?>
    </ul>
    <?php endif;
    $wp_query = null;
    $wp_query = $original_query;
     wp_reset_postdata(); 
    } // end if($terms)