0

I have created a custom taxonomy for a custom post. Now i want to show all post related to a particular term. Suppose i have two terms term1 and term2. When click on term1 then all post related to term1 will show and post , that is related to term2 will not show. But now when i click term1 then post related to term1 and term2 are showing at a time. I have wrote the following code to taxonomy.php template.

        <div class="main-content">
                <?php
                $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

                    $the_query = new WP_Query( array(
                        'post_type' => array('newsbox_post'),
                        'tax_query' => array(
                            'taxonomy' => $term->taxonomy,
                            'field' => 'slug',
                            'terms' => $term->name,
                        ),
                    ) );

                ?>
                <?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
                    <div class="post">
                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                            <?php the_content(); ?> 
                            <?php echo get_the_term_list( $post->ID, $term->taxonomy, 'People: ', ', ' );  ?>
                            <hr />
                    </div>

                <?php 
                    endwhile;
                    wp_reset_postdata();
                    else:?>
                        <h3><?php _e('404 Error&#58; Not Found'); ?></h3>
                <?php
                    endif;
                ?>  
            </div>

Please tell me , how can i achieve that.

Bir
  • 794
  • 1
  • 15
  • 31

2 Answers2

0

You should use slug instead of name like following code:-

$the_query = new WP_Query( array(
                    'post_type' => array('newsbox_post'),
                    'tax_query' => array(
                       array(
                            'taxonomy' => $term->taxonomy,
                            'field' => 'slug',
                            'terms' => $term->slug,
                       ),
                    ),
                ) );

Hope this will help you.

Akshay Paghdar
  • 3,599
  • 2
  • 22
  • 41
  • You got the point @Akshay PãghdÂr . But i have made another mistake. Check my answer – Bir Sep 28 '14 at 16:43
0

I have got the answer .

                    $the_query = new WP_Query( array(
                        'post_type' => array('newsbox_post'),
                        'tax_query' => array(
                            array(
                                'taxonomy' => $term->taxonomy,
                                'field' => 'slug',
                                'terms' => $term->slug,
                            ),
                        ),  
                        ) );
Bir
  • 794
  • 1
  • 15
  • 31