views:

28

answers:

2

Hi all. Does anyone know how to display 3 previous posts on article page? Let's say I'm in article post page and would like to display previous 3 posts images (basing on the post I'm currently reading) below it's content, but not next posts images. I was trying to do this with setting up the offset in the db query but with no luck. Can someone please help me with this one? I would greatly appreciate any help or at least pointing me in the right direction.

+1  A: 

That shouldn't be too hard, if you use an id field with auto increment values, you could do something like this:

SELECT id, name FROM articles WHERE id < {current_id} ORDER BY id DESC LIMIT 3;

Obviously, replace {current_id} with the id of the article you're currently reading.

GuidoH
You're right GuidoH - it's pretty easy when it comes to form a db query. However how to do it using wordpress functions?
Pavel
@Pavel: How about adding that to your question? Because "I was trying to do this with setting up the offset in the db query but with no luck." suggests that the problem is to actually retrieve them with a query.
GuidoH
I'm very sorry if I wasn't precise enough. The db query in wordpress looks like this <?php $my_query = new WP_Query("showposts=1 ?> Can you figure out something from that?
Pavel
Just a note: this assumes the order of posts by ID is the same as the publication date. If you use concepts and publish in a different order the 3 previous may be different.
Kwebble
+1  A: 

After displaying the specific post do a new WP_Query to retrieve the 3 posts previous to the publication date of the displayed post. This documentation page describes how to query for posts with a relative date (e.g. the example Return posts from the last 30 days). Base the query on the publication time of the displayed post.

The example includes a way to supply a WHERE clause to the query, with add_filter(). I'm not sure, but I think you need to call remove_filter after doing the query, or that filter might be applied to other queries also.

Kwebble