How to Display Recent Custom Post Types (Excluding the Current Post)

Let’s say you’re working on a page template and it’s going to include a partial that’s going to display recent custom post types (or maybe even the standard post type) of the post type you’re viewing.

For example, say you’re looking at the example of stories or something like that. And you’re looking at a single page for a given author. Then, perhaps in the sidebar, it reads Read More From Other Authors, and it retrieves stories from up to five other authors.

Display Recent Custom Post Types
Where additional custom post types may appear.

Writing a custom WP_Query, pulling back the five most recent posts, and dropping them into the selected area seems easy enough, right?

And it is. But there’s a gotcha.

Display Recent Custom Post Types

Initially, the query may look something like this:

But remember: We’re looking at an author’s page, and we want to pull the results of up to five other authors. To do this, it’s important to exclude the current story from the query. Thus, the query becomes:

And now you’ll get up to the five most recent stories from authors excluding the author that you’re currently viewing.

It’s Too Simple

I know, it sounds a bit simplistic, but it’s something others have encountered (including myself, for whatever it’s worth); otherwise, I’d not bother writing about it.

The short of it is that if you’re looking list out a set of recent entries excluding the post you’re already on, make sure that you use the post__not_in directive in your custom query.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.