Title: Loop inside sidebar?
Last modified: August 19, 2016

---

# Loop inside sidebar?

 *  [kab2512](https://wordpress.org/support/users/kab2512/)
 * (@kab2512)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/loop-inside-sidebar/)
 * Is it ok to make a loop inside a sidebar?
    Or should it do otherwise?
 *     ```
       <?php
   
       while ( have_posts() ) : the_post(); 
   
       	$post_id = $wp_query->post->ID;
   
       $random = get_post_meta($post_id, 'random', true) ?> 
   
             <?php echo $random; ?> 
   
       <?php endwhile;
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/loop-inside-sidebar/#post-1905818)
 * better to use a custom query loop
 *     ```
       <?php
         $sidebarPosts = new WP_Query();
         $sidebarPosts->query('posts_per_page=5&cat=3,7,12');
         while ($sidebarPosts->have_posts()) : $sidebarPosts->the_post(); ?>
            $post_id = $wp_query->post->ID;
            $random = get_post_meta($post_id, 'random', true);
            echo $random;
         endwhile;
       ?>
       ```
   
 *  Thread Starter [kab2512](https://wordpress.org/support/users/kab2512/)
 * (@kab2512)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/loop-inside-sidebar/#post-1905837)
 * Thank you for your answer. Very helpful
 * Shouldnt it be
 *     ```
       <?php
         $sidebarPosts = new WP_Query();
         $sidebarPosts->query('posts_per_page=5&cat=3,7,12');
         while ($sidebarPosts->have_posts()) : $sidebarPosts->the_post(); ?>
        $post_id = $sidebarPosts->post->ID
        $random = get_post_meta($post_id, 'random', true);
        echo $random;
         endwhile;
       ?>
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Loop inside sidebar?’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [kab2512](https://wordpress.org/support/users/kab2512/)
 * Last activity: [15 years, 4 months ago](https://wordpress.org/support/topic/loop-inside-sidebar/#post-1905837)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
