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

  • Home
  • SEARCH
  • 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 893597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:14:53+00:00 2026-05-15T14:14:53+00:00

I’ve got a little sidebar that displaying in the code before the main page

  • 0

I’ve got a little sidebar that displaying in the code before the main page content. I wrote this (janky) function to pull in recent posts and comments. Works great, however, its screwing with all my pages and putting posts on all of them. How do I reset/rewind the query or make a new one so all my pages display the correct content?

<?php rewind_posts(); ?>and<?php wp_reset_query(); ?> arent doing the trick for me

Here is my query:

$comments = get_comments('number=10');
$posts    = get_posts('posts_per_page=10&category=6');


$most_recent = array();

foreach ($comments as $comment)
    $most_recent[strtotime($comment->comment_date_gmt)] = $comment;

foreach ($posts as $post)
    $most_recent[strtotime($post->post_date_gmt)] = $post;

unset($comments, $posts);

krsort($most_recent);

$most_recent = array_slice($most_recent, 0, 10);

foreach ($most_recent as $post_or_comment) {

    $is_post  =    isset($post_or_comment->post_date_gmt);
    $comment_id =   $post_or_comment->comment_ID;
  $post_id =    $post_or_comment->ID;  
 $comment_post_id =   $post_or_comment->comment_post_ID; 

 if ($is_post == 1) 

   { ?> <li><a href="<?php  echo get_permalink($post_id); ?>"><?php echo $post_or_comment->post_title; ?></a><span class="tag"><?php echo the_category($post_id); ?></span></li><?php } 

 else 
   { ?><li> <a href="<?php  echo get_permalink($comment_post_id); ?>"><?php echo get_the_title($comment_post_id); ?></a><span class="tag">Comment</span></li><?php }

    // output comments and posts
}

output comments and posts
}

  • 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-15T14:14:54+00:00Added an answer on May 15, 2026 at 2:14 pm

    Try using $my_posts and $my_comments instead, just in case WP is using samely-named globals (though I don’t think it is).

    Also, in your foreach loop, you should only be casting variables when you know the object type, otherwise you’re accessing non-existent properties;

    foreach ($most_recent as $post_or_comment) {
    
        $is_post  =    isset($post_or_comment->post_date_gmt);
        if ($is_post) {
            $post_id =    $post_or_comment->ID;  
        } else {
            $comment_id =   $post_or_comment->comment_ID;
            $comment_post_id =   $post_or_comment->comment_post_ID; 
        }
    }
    

    rewind_posts() will have no effect here. Just call wp_reset_query() right after the foreach loop ends.

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

Sidebar

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.