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 7520391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:03:45+00:00 2026-05-30T02:03:45+00:00

I store post IDs in an array. I would like to loop through the

  • 0

I store post IDs in an array. I would like to loop through the array and display the IDs within a <div> containing <p> and <ul> tags, but only when at least one ID is in the array. If the array is empty no html can be returned. This implies that I should use some kind of if statement before the loop. Needless to say, my php skills are pretty basic and after two days of trying hard I am getting nowhere. Grateful for help!

My code (using WordPress)

$postids = array();

...

$postids [] = $post->ID; //stores the post IDs in the array

Here is an update. I apologize for posting all this code as its quite messy with many things going on. It’s the second loop of three (or more). The IDs displayed in a near identical first loop have been passed on. Only those IDs which have not been retrieved by the previous loop are displayed in order not to show any duplicate posts.

I have tried to remove all HTML markup and query the $postids with a new WP_Query after but that retrieves all posts I have ever created. I am pretty sure that’s the right way to continue although I am obviously doing something wrong.

<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
  $first_tag = $tags[1]->term_id;
  $args=array(
    'tag__in' => array($first_tag),
    'post__not_in' => array($post->ID),
    'showposts'=>5, //Display this number of related posts
    'ignore_sticky_posts'=>1
   );
  $postids = array();
  $my_query = new WP_Query($args);
  if( $my_query->have_posts() ) {
      echo '<ul id="relatedposts">'; 
      while ($my_query->have_posts()) : $my_query->the_post(); if (!in_array($post->ID, $ids)) {; $postids [] = $post->ID; ?>
      <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
      <?php }
    $ids[]= $post->ID; 
    endwhile;
  }
}
?>
      </ul>
<?php if ($postids){ //$postids has at least one value set
    echo '<div>Related posts</div>'; //Outputting the header text. This works! If there are no IDs in the array nothing is shown.
     }; 
?>
  • 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-05-30T02:03:46+00:00Added an answer on May 30, 2026 at 2:03 am

    This should work:

     <?php
    
     // assuming you have an array of ids called $postids
    
     if(count($postids)){
        echo "<div><ul>";
        foreach($postids as $id){
           echo "<li>$id</li>";
        }
        echo "</ul></div>";
     }
    
    
     ?>
    

    To break it down:

    if(count($ids)){
    

    count() returns the number of elements in the array $ids. Any number other than zero will evaluate to true and enter the if statement, zero will evaluate to false and the whole thing will be skipped.

     foreach($ids as $id){
    

    This loops through each element in the array $ids and assigns it to the variable $id. Hopefully the echo statements are self explanatory.

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

Sidebar

Related Questions

I want to store blog and its tags as separate documents. Blog post should
I store serialized data in the registry. I want to use a foreach loop
Sorry for the long post, but most of it is code spelling out my
I have a simple PHP Array called $categories that looks like this: Array (
I'm using acts_as_taggable_on in an app and I would like to extract the tag
how do I post an array to an action on my controler with the
Excuse the title of this post, but I can't really think of a more
I'm making a simple online store like program. What can you suggest that I
If anybody has a similar story, please post details below! I'm building an ASP.NET
Store everything in GMT? Store everything the way it was entered with an embedded

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.