Title: Loop not working
Last modified: August 19, 2016

---

# Loop not working

 *  [Rub3X](https://wordpress.org/support/users/rub3x/)
 * (@rub3x)
 * [17 years ago](https://wordpress.org/support/topic/loop-not-working-1/)
 * Ok I want to call the loop in the sidebar and run a custom function on each post
   ID.
 * Currently when I call the function it loops through all the posts and will echo
   every post number, but the image it displays is the first image everytime.
 * I echoed out $post->ID and it returns the correct post ID for each loop, just
   the wrong image.
 * [http://pastebin.ca/1449213](http://pastebin.ca/1449213)
 * There’s the get image function in case there’s an issue with that.
 * Sidebar code:
 *  mdv_recent_posts();
    function mdv_recent_posts($no_posts = 5, $before = ‘
    - ‘, $after = ‘
    - ‘, $hide_pass_post = true, $skip_posts = 0, $show_excerpts = false, $include_pages
      = false) {
       global $wpdb; $time_difference = get_settings(‘gmt_offset’); $
      now = gmdate(“Y-m-d H:i:s”,time()); $request = “SELECT ID, post_title, post_excerpt
      FROM $wpdb->posts WHERE post_status = ‘publish’ “; if($hide_pass_post) $request.
      = “AND post_password =” “; if($include_pages) $request .= “AND (post_type=’
      post’ OR post_type=’page’) “; else $request .= “AND post_type=’post’ “; $request.
      = “AND post_date_gmt < ‘$now’ ORDER BY post_date DESC LIMIT $skip_posts, $
      no_posts”; $posts = $wpdb->get_results($request); $output = ”; if($posts) {
      foreach ($posts as $post) { echo get_the_image( ‘post_id=’ . $post->ID ); 
      echo $post->ID; } } else { $output .= $before . “None found” . $after; }
    - The output can be found on the right sidebar:
    - [http://joe.cyber-knowledge.net/](http://joe.cyber-knowledge.net/)

Viewing 1 replies (of 1 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years ago](https://wordpress.org/support/topic/loop-not-working-1/#post-1096487)
 * Might look at using [Function_Reference/get_children](http://codex.wordpress.org/Function_Reference/get_children)

Viewing 1 replies (of 1 total)

The topic ‘Loop not working’ is closed to new replies.

 * 1 reply
 * 2 participants
 * Last reply from: [MichaelH](https://wordpress.org/support/users/michaelh/)
 * Last activity: [17 years ago](https://wordpress.org/support/topic/loop-not-working-1/#post-1096487)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
