First, modify your example by specifying post type post
and removing the cat
and post__not_in
arguments. Next, if you want to make sure you’re looking at the parent’s date and not the media’s date, you’ll also need to make sure to have a fallback in case someone tries to view an image that isn’t attached to a post (and therefore doesn’t have a parent).
// Make sure we can access the current $post, which is media
global $post;
// If the media isn't attached
if($post->post_parent == 0) {
// Use the media itself's upload date
$date = $post->post_date;
} else {
// Get the parent post
$parent = get_post($post->post_parent);
// Use its date
$date = $parent->post_date;
}
// Get 2 Posts
$qry = new WP_Query(
array(
// This pulls exactly 2 posts
'posts_per_page' => 2,
// This pulls only Posts
'post_type' => 'post',
// This finds posts published before the current item
'date_query' => array(
array(
'before' => $date,
),
),
)
);
You’ll then need to do something with the results. You can start with a simple print_r($qry);
to make sure you retrieved the posts you intended to, then move on to a custom loop to actually display them:
if($qry->have_posts()):
while($qry->have_posts()) : $qry->the_post();
// Set up whatever html structure you want here ?>
<hr/>
<article>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</article><?php
endwhile;
endif;
This will create a horizontal rule, then show the title and content of the first post, then another rule, then title and content of the second post.