I have 7 posts, like this:
1
2
3
4 – this is the current post
5
6
7
As noted, number four is the current post being displayed. I need to create a query that will allow me to display the previous 3 posts (by publication date) and also the three posts after. This can be done with two separate queries.
I’ve been able to display the immediately previous or next post, just not the ones further down / up.
Any ideas?
This can be done in a single query, though i can’t speak specifically about how well this query will perform(i’ve not spent a great deal with Union queries – never had a need, till now)..
First, a function to select two sets of results, but using union to return them as a single result set.
There’s a test date in there, you can safely ignore that or add in your own value for testing.
Here’s some sample code you can use in your single.php loop to list out the results, though note this is just a generic example, and the function might need to select more/different data, but based on the info you’ve provided i wasn’t sure exactly what you wanted, so the following is for illustration and to give a sample you can use to test the results..
Awaiting feedback… 🙂
This can be done much better with the date_query property of WP_Query class. This will get the posts before the publish date of the current post
………
3 x get_adjacent_post():
same for next 3 posts, just change the function to get_next_post()…
To do this with a single query, while still using the WP API, try changing the
LIMIT
value to 3 within theget_previous_post_sort
andget_next_post_sort
filters.As JanFabry suggests in an answer to @onetrickpony above, you can modify get_adjacent_post(). This is what I did. Here’s the function. I changed the function signature because it made more sense to me this way.