Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8351883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:47:04+00:00 2026-06-09T08:47:04+00:00

I did a search not only on Google but other places, including here and

  • 0

I did a search not only on Google but other places, including here and cannot find anything to help with this issue.

Here is the problem. I have a related posts snippet based on tags instead of categories, that I use in WordPress themes and I have been using it for quite some time and it works really well. Here is is:

        $tags = wp_get_post_tags($post->ID);
    $tagIDs = array();
    if ($tags) {
        $tagcount = count($tags);
        for ($i = 0; $i < $tagcount; $i++) {
            $tagIDs[$i] = $tags[$i]->term_id;
        }
    $args=array(
    'tag__in' => $tagIDs,
    'post__not_in' => array($post->ID),
    'showposts'=>mytheme_option( 'related_count' ),
    'ignore_sticky_posts'=>1
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
        echo '<h4>'. __('Other Posts You May Be Interested In', "themename"). ':</h4><ul>';
        while ($my_query->have_posts()) : $my_query->the_post(); ?>

            <li class="imglink">

                <!-- post loop stuff goes here -->

            </li>

<?php endwhile;
echo '</ul>';
}
else {
echo '<h4>'. __('Other Posts You May Be Interested In', "themename"). ':</h4>
'. __('<p>There are no related posts at this time.</p>', "themename"). '';
}   
}
$post = $original_post;
wp_reset_query();

Like I said it works really well. If posts have the same tag, then this shows:

Other Posts you May Be Interested In:
posts with the same tag get displayed

BUT here is the problem: If a single post is given a tag and no other posts have that same tag, this is what shows:

Other posts you may be interested in: There are no related posts at
this time.

Now if a post is not assigned a tag, absolutely nothing shows. The div where related posts are supposed to display is empty but it should say there are no related posts.

I have looked for a solution and tried many different things to get this corrected but I can’t seem to get my head around it. Can someone assist me in getting:

Other posts you may be interested in: There are no related posts at
this time.

to display if a post has no tag. Any help is greatly appreciated and thank you very much in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:47:05+00:00Added an answer on June 9, 2026 at 8:47 am

    I believe the problems comes from the conditional towards the top of the function:

    $tags = wp_get_post_tags($post->ID);
    
    $tagIDs = array();
    if ($tags) {
        # All of your code is in this conditional, 
        # if there are no tags, nothing will happen
    }
    

    If no tags are found for the post, it skips all your code and doesn’t render anything. The else statement at the bottom of your code is only executed if there are tags for the current post, but no other posts contain that tag.

    Edit with example as requested – you need to have 2 else conditions which echo out the no posts content, one for if there are no tags and one for if there are tags but posts were not found:

    $tags = wp_get_post_tags($post->ID);
    
    $tagIDs = array();
    if ( count( $tags ) > 0) {
        // setup $tagIDs and perform query...
    
        if( $my_query->have_posts() ) {
            // Echo out posts.
        } else {
            echo '<h4>'. __('Other Posts You May Be Interested In', "themename"). ':</h4>
            '. __('<p>There are no related posts at this time.</p>', "themename"). '';
        }
    } else {
        echo '<h4>'. __('Other Posts You May Be Interested In', "themename"). ':</h4>
        '. __('<p>There are no related posts at this time.</p>', "themename"). '';
    }
    

    That should do it. I would maybe suggest wrapping the content into a function, however. Then you could just echo out the no post content at the end of the function, and ‘return’ if posts were found. Then you wouldn’t be repeating yourself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Probably this question was already asked before, but my google-fu and SO-Search did not
I did search the forum and did not find a similar question. I'm looking
Please note that this is not homework and i did search before starting this
First of all, I did a search on this and was able to find
I did a lot search already but couldn't find a straight anwser. I have
My apologies if this is repetitive but I did search for an answer to
I did a search and was not able to find it. We have a
just to put it out there -I really did search and try to find
I did a search on the elements of the list, but the problem is
First of all, I did some search and no answer on stackoverflow/google provided me

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.