I have a simple issue that can’t solve.

I have a Custom Post Type called “Hardware” and inside a taxonomy called “hardware_category”. I'm trying to display a list of my custom posts in each term with the file taxonomy-hardware_category.php but doesn’t return me any result (it should display 5 posts).

Anything special to consider in my template?



// File: taxonomy-hardware_category.php

$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

<div class="main">

    <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>

        <div class="product">
            <?php the_post_thumbnail('full'); ?>
            <?php the_title(); ?>

        <?php endwhile; ?>
    <?php endif; ?>


<?php get_footer(); ?>
  • Are you trying to display a list of posts in each term, or a list of terms themselves? Commented Feb 15, 2020 at 2:00
  • Trying to display a list of posts in each term, thanks for answer. Commented Feb 15, 2020 at 2:23

1 Answer 1


Well, first of all, you are not doing anything with the $term variable. You are querying it, but then it sits there, waiting to be picked up.

Try this code in your taxonomy template, called taxonomy-hardware_category.php:

// prepare the variables for the query
$tax = get_query_var('taxonomy');
$term = get_query_var('term');

// Setup the loop, build the html, return it, clean up the query
    $hardware_args = array(
        'post_type' => 'hardware',
         // This should get you the result
         // the two arrays isn't a mistake
         'tax_query' => array(
                            'taxonomy' => $tax,
                            'field' => 'slug',
                            'terms' => $term
                    'posts_per_page' => '30',
                    'orderby' => 'name',
        $loop = new WP_Query($hardware_args );
        $html = '';
        while ($loop->have_posts()) : $loop->the_post();

        $html .= '<div class="some-custom-class-like col-md-12">';
            $html .= '<div class="hardware">';
                $html .= '<a href="'.get_the_permalink().'" class="permalink" title="'.get_the_title().'">';
                $html .= '<div class="thumbnail-wrap>';
                    if(has_post_thumbnail()) {
                        $html .= '<img src="'.get_the_post_thumbnail_url().'" class="img-fluid" alt="'.get_the_title().'&nbsp;'.__('Hardware preview', 'textdomain').'">';
                            $html .= '</div>';
                        $html .= '</a>';
                        $html .= '<a href="'.get_the_permalink().'" class="title-permalink btn btn-outline-primary" title="'.get_the_title().'"><span class="the-title">'.get_the_title().'</span></a>';
                        $html .= '<div class="hardware-metadata">';
                        // Some more metadata can be put here for example
                        $html .= '</div>';
                    $html .= '</div>';
                $html .= '</div>';

                // Echo it all out
                echo $html;
  • Thanks thanks man! For your speed, clarity and time. It worked perfect :) Commented Feb 15, 2020 at 3:04
  • You are welcome :) Glad I could help. Commented Feb 15, 2020 at 3:05
  • Is there anyway i can add a pagination? Commented Feb 15, 2020 at 3:34
  • This answer is completely incorrect. You should not be using custom queries for taxonomy archives. People doing this is the number one mistake people are making when posting about their issues on this site. Something else is wrong. Commented Feb 15, 2020 at 4:47
  • Then please enlight us with the answer. Moaning it is wrong is fine, but then not giving a solution is wrong either. Commented Feb 15, 2020 at 12:23

Your Answer

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

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