(Moderator’s note: The question’s original title was: “Custom query: Show custom post types plus custom post type with post meta on blog homepage”)
I’m in need of help with a custom query. I have three custom post types: “News”, “Events”, “Stories”. On the main blog page, only News and Events will show. The client can choose to mark a Story as News using features of the plugin Verve Meta Boxes. If a Stories post has a post meta key of 'mark-as-news'
then the that post should display on the main blog page as well.
So what I need help with writing is this:
If Stories has post meta
'mark-as-news'
then the main blog
page will display custom post types
News and Events, as well as Stories,
in descending order.
Can someone please assist?
Thanks!
Edit:
Perhaps a better way of wording this would be?:
Show custom post types News and
Events, and if there is the key
'mark-as-news'
forpost_meta
, show
Stories.
Hi @webcodeslinger:
Here’s what a basic MySQL query for loading News and Events would look like (this is greatly simplified from what WordPress actually does):
What you want instead is something like this (there are more performant ways to do this in MySQL but they are more complex in WordPress and for most sites you’ll never notice a difference):
So…to add that extra SQL to your main loop’s query use a
'posts_where'
hook. You can put the following hook code into your theme’sfunctions.php
file (at the bottom of the file will do) or in one of the.php
files of a plugin if you are creating a plugin:What you want is easy to describe, but hard to get working with WP API. I hadn’t seen nice and tidy solution so far.
Basically WP query is a fancy wrapper for SQL query that gets post rows from database. What you want is practically two different queries for two different sets of posts. WP doesn’t currently handle such OR logic in queries.
You can make two queries and combine results, but in that case it will be hard to get pagination right.
This is probably one of the reason magazine-style themes became popular, because they specialize in showing multiple sets of posts in several areas, rather than struggling with complex mix of content.