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.phptemplate.

             <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.

1 Answer 1

1

It is not advised to run custom queries in place of the main query. What you are trying to do is already done by the main query. In short, you are trying to reinvent the wheel :-).

Your no 1 solution would be to return to the default loop, and apply changes to the main query through pre_get_posts. Check this post for more info

Just on your code, you are passing the term name to your terms parameter, where as you should be passing the term slug as you have set your field to slug. Also, your tax_query should be an array of an array. Change your code to

 $the_query = new WP_Query( array(
    'post_type' => array('newsbox_post'),
    'tax_query' => array(
        array(
            'taxonomy' => $term->taxonomy,
            'field' => 'slug',
            'terms' => $term->slug,
        ),
    ),  
) );
3
  • Same result @Pieter Goosen
    – Alamin
    Sep 28, 2014 at 15:51
  • I want to show post from custom post. For that reason i have used custom queries. It is not altering the default query. It's just create a object of query. I think, query is not problem. You can check it here tutsmix.com/…
    – Alamin
    Sep 28, 2014 at 16:02
  • Sorry about that. I'm in such a hurry, I missed one big problem in your code. See my edit Sep 28, 2014 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.