I have a custom category template which was working correctly, showing 6 different category posts. After I added two new categories, all went berserk and 2 category pages are showing incorrectly filtered posts even though all category pages still show the correct category title in the H1. I am using template parts for several html blocks.

Where could I start checking for errors?

I also noticed with

$category = get_the_category('');

that one of the categories now has a category parent, which I might have erroneously selected, but deselecting it apparently did not fix the relationship.

This is my main category post template:


$category = get_the_category();
$catTerm = $category[0]->cat_name;

$args = array(
    'post_type' => 'post',
    'posts_per_page' => 1,
    'featured' => 'featured',
    'post_status' => 'publish',
    'category_name' => $catTerm

    $lastPost = new WP_Query($args);

    if( $lastPost->have_posts() ):
    echo '<div id="mainArticleBox">';
    while( $lastPost->have_posts() ): $lastPost->the_post();
    //  get_template_part('content', 'featured-big');
    if (has_post_thumbnail()) :
    $category = get_the_category( $id );

    echo '

    <div class="hero-img" style="background-image: url('
        . get_the_post_thumbnail_url() . 
            <div class="tag-id hidden-xs hidden-sm">';
                    $category = get_the_category();
                    echo '<a href="'.get_category_link($category[0]->cat_ID).'">' . $catTerm. '</a>
        echo '
            <div class="mainArticle-texts">
                <h1 class="h1"><a href="' . get_permalink() . '">' . get_the_title(). '</a></h1>
                <div class="excerpt"><a href="' . get_permalink() . '"> <span>' . excerpt(30) . '</span> </a></div>

  • Where is the loop? Also, try to keep your code brief. Boil it down to the absolute minimum you need us to see to illustrate the error, that makes it much easier to read and so makes it more probable that somebody will help you.
    – janh
    Jan 19, 2018 at 16:44
  • I switched the code and added a new bit of info I found. Thanks.
    – MauF
    Jan 19, 2018 at 16:55
  • Why are you creating a new query rather than using the main query?
    – Milo
    Jan 19, 2018 at 16:58
  • Well, I am reusing that query because that template part work in several sections of the site. Is it wrong? Also, with that query I get several terms that I output via HTML.
    – MauF
    Jan 19, 2018 at 17:00

1 Answer 1


category_name expects a category slug, albeit the name of the parameter.

get_the_category should be used either with an ID or inside the loop - it returns the categories for the given/current post. But even if that worked, you'll get an array of WP_Term objects - cat_name is not a property. User ->name to get the name, or ->slug to get the slug.

If you know the category ID or slug, you can simply use get_term_by, e.g.

if($term = get_term_by("id", $id, "category")) {
    // set your arguments for WP_Query
    $args["cat"] = $term->term_id;
  • Thank you for your help! I did it that way because I asumed that each category page will give me the current category term, with worked correctly for every previous category name I had, until I added two categories a few days ago. Why, then, it worked and still works correctly for some, but not all, categories? I am using var_dump($catTerm); on each category page and several show the correct category, but not all. Why is this happening?
    – MauF
    Jan 19, 2018 at 17:15
  • My guess is that another loop (possibly the main loop) is running and gives you a post from that category, and by accident, that matched most of the time / always when you had only two categories. If that outer loop gives you a post without categories or another category, it fails. To get the currently shown category on a category archive page, use get_queried_object.
    – janh
    Jan 19, 2018 at 17:21
  • That makes a TON of sense. I will implement my query the correct way. Thank you!
    – MauF
    Jan 19, 2018 at 17:25

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.