SQL for SELECT previous with AND

Hi,

I store my posts and pages in one table using a post_type column to assign post type (post, page) as follows:

post_title | post_type
Post 1 | post
Page1 | page
Post 2 | post
Post 3 | post
Page 2 | page

I display posts and pages on the same page (PHP file). I need Previous - Next links on that page. If the current page is post_type = post, the previous link should display the previous post_type = post entry. If it is a post_type = page, it should display the previous post_type = page entry.

I tried the following query:

SELECT post_name FROM posts WHERE post_id = (SELECT MAX($id) FROM posts WHERE post_id < $id AND post_type = 'post')

$id is the current post’s id. The above query returns the current entry. I need it to return the previous entry of the same post_type. Any ideas how to modify it to accomplish what I am trying to do?

Thanks.

EDIT - SOLVED: Just noticed I needed to use MAX(post_id) instead of MAX($id), which fixed the issue.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.