Support » Fixing WordPress » The function has_term() is evaluating to both true and false.

  • Resolved CoBu1

    (@cobu1)


    I’m having trouble with the has_term() function in WordPress. For some reason it is evaluating to both true and false, showing both the content-deal.php and content.php templates. What am I doing wrong?

    if( have_posts() ) :
        while( have_posts() ) : the_post(); 
    
            // Create a different layout if it's a Members Exclusive
            if( has_term('member-exclusives', 'offer-type') ) {
    
                get_template_part('templates/content', 'deal');
    
            // Otherwise, use this layout
            } else {
    
                get_template_part('templates/content');
    
            }
        endwhile;
    endif;
Viewing 6 replies - 1 through 6 (of 6 total)
  • As you’ve got it in a loop, it looks like you’ve got more than one post that’s being output. That means that one (or more) has the term, and one (or more) doesn’t have the term.

    Thread Starter CoBu1

    (@cobu1)

    Thanks for your help!

    This is on a single-posttype.php template, so there should only be one post.

    Yes, should be… But that’s the most obvious thing that could be going wrong.

    I’d suggest adding in some de-bugging text, something like this:

    if( have_posts() ) :
        while( have_posts() ) : the_post();
    echo "<p>Outputting post '".$post->ID."'</p>";
    
            // Create a different layout if it's a Members Exclusive
            if( has_term('member-exclusives', 'offer-type') ) {
    
                // get_template_part('templates/content', 'deal');
    echo "<p>Output DEAL template for post '"$post->ID.."'.</p>";
    
            // Otherwise, use this layout
            } else {
    
                // get_template_part('templates/content');
    echo "<p>Output STANDARD template for post '".$post->ID."'.</p>";
    
            }
        endwhile;
    endif;

    You can add in more as needed, but that’s a start, and it will show what’s happening.

    Thread Starter CoBu1

    (@cobu1)

    Thanks again. That was a great suggestion.

    I’ve realized that this is only an issue with one specific post, and indeed the issue is that the query is loading 2 posts. It looks like my client made these two posts with the same title, but when I put one of the posts into draft mode, nothing changes. It still gets outputted.

    Not that it’s a big deal, but do you have any idea how that duplicate post gets into the default query?

    I don’t know all of the internals, but if the two posts have the same title, and your permalink strucutre is set to postname, the WordPress internals will try to find posts that have that title, so it seems like when there’s two with the same title they are both being selected somehow. It doesn’t seem quite right as the URL should be what’s used to select the proper single ID, but like I said I haven’t delved that far into the internal workings at this point.

    Thread Starter CoBu1

    (@cobu1)

    No problem. Thank you so much for your help!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘The function has_term() is evaluating to both true and false.’ is closed to new replies.