In the context of the website redesign of the School of Computer Graphic Design that i teach in, i had the need to display on the frontpage a link to the latest grandchild of a page. It didn’t turn out as easy as i thought, but i somehow managed it. Here is how.

I looked around for any built-in function or plugin but nothing suited my need. Eventually, i resorted to doing a specific query in the database.

Here is the query and the loop that uses the dataset, as they sit in my frontpage.php template. It results in displaying a link with the latest testimonial’s image to make it stand out.

[PHP]
$querystr = “SELECT p3.*

FROM $wpdb->posts p1
LEFT OUTER JOIN $wpdb->posts p2 ON p2.post_parent=p1.ID
LEFT OUTER JOIN $wpdb->posts p3 ON p3.post_parent=p2.ID
WHERE (p1.post_status = ‘publish’ AND p1.post_type = ‘page’ AND p1.ID =’28’) AND (p3.post_status = ‘publish’ AND p3.post_type = ‘page’) ORDER BY p3.post_date DESC LIMIT 0,1”;

$myposts = $wpdb->get_results($querystr, OBJECT);
if ($myposts) {

foreach($myposts as $post) :
setup_postdata($post);
$postimageurl = get_post_meta($post->ID, ‘post-img’, true);
$postimageurl = ($postimageurl) ? $postimageurl : ‘/medias/img/temoignage-banner.jpg’;
?>

< ?php the_title(); ?>

< ?php endforeach; ?>
< ? } ?>
[/PHP]

If you have any question, shoot!

[ad#horizontal_inline]