Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

mdudu2 on "Custom sql wordpress query with pagination..."

$
0
0

Hello,

I've done a custom sql query for my blog. What the code does is to retrive the latest image from a post that has a gallery. In this way i can display latest ... les't say 12 posts that have a gallery- the code actualy displays the first image that is attached to the post.

Problem is that i cannot create a next prev functionality for this and i really need this

Here is the code:

<?php wp_reset_query();
 global $wpdb;
 $posts = $wpdb->get_results
 ("
 SELECT *
 FROM $wpdb->posts
 WHERE
 post_status = 'publish'
 AND
 ID IN (
            SELECT DISTINCT post_parent
            FROM $wpdb->posts
            WHERE
              post_parent > 0
            AND
              post_type = 'attachment'
            AND
              post_mime_type IN ('image/jpeg', 'image/png')
  )
ORDER BY post_date DESC LIMIT 0, 12
");

foreach($posts as $post) :
setup_postdata($post);
?>

<?php
$images = get_children(array(
  'post_parent' => get_the_id(),
  'post_type' => 'attachment',
  'post_mime_type' => 'image',
  'orderby' => 'menu_order',
  'order' => 'ASC'
));
$ids = array_keys($images);
            ?>

   <div style="height:132px; width:132px; float:left; margin-right:1px; margin-top:1px; overflow:hidden;" ><?php
  echo the_attachment_link($ids[0],false, false, true);
            ?></div>

<?php
endforeach;
wp_reset_query();
?>

Thank you


Viewing all articles
Browse latest Browse all 8245

Trending Articles