I am using Tribulant Gallery Voting to have a Pet contest on my website.

The plugin adds a Vote button below each image in the gallery.

I want to show the 'Top 5' Pet photos with most votes in the sidebar.

My friend has written some code that extracts Votes, image id, title and description from the Tribulent gallery voting database table and sorts them in descending order (most votes first)

  global $wpdb;
  $result = $wpdb->get_results( "SELECT attachment_id, COUNT(attachment_id) AS qty FROM wp_5vt0txsx66_galleryvotes GROUP BY attachment_id ORDER BY qty DESC" );

  echo "<table>";
  echo "<tr><td>Qty</td><td>image_id</td><td>Project Title</td><td>Project Description</td></tr>";

  foreach ($result as $row)
 echo "<tr>";

 echo "<td>".$row->qty."</td><td>".$row->attachment_id."</td>";

 $attachment_meta = wp_get_attachment($row->attachment_id);

 echo "<td>".$attachment_meta['title']."</td>";

 echo "<td>".$attachment_meta['caption']."</td>";

 echo "</tr>";

 echo "</table>";

 function wp_get_attachment( $attachment_id ) {

 $attachment = get_post( $attachment_id );

 return array(

      'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),

      'caption' => $attachment->post_excerpt,

      'description' => $attachment->post_content,

      'href' => get_permalink( $attachment->ID ),

      'src' => $attachment->guid,

      'title' => $attachment->post_title



Here Qty represents Votes.

The above code displays the following output in the sidebar : enter image description here

I want to show the Image itself in place of image_id. Can anyone please guide me how to get the images from the database and display them.

1 Answer 1


You can use get_attachment_image():

$att_markup = wp_get_attachment_image( $attachment->ID, 'thumbnail' );

Or get_attachment_link():

$att_link = wp_get_attachment_link( $attachment->ID, 'thumbnail' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.