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
You need to put
$paged
parameter into your code and then call theprevious/next
links. See this: https://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_queryTherein lies my issue! That method seems to work when the wpdb object is used, but in my case it’s an isolated PHP PDO/SQL call that doesn’t use the query object 🙁