(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?



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' for post_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):

SELECT * FROM wp_posts WHERE post_type IN ('news','events') 

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):

SELECT * FROM wp_posts WHERE post_type IN ('news','events') 
  OR (post_type='stories' AND ID IN 
    (SELECT post_id FROM wp_postmeta WHERE meta_key='mark-as-news')

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's functions.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:

function yoursite_posts_where($where,$query) {
  global $wp_the_query;
  if (is_home() && $query===$wp_the_query) { // This means home page and the main loop
    global $wpdb;
    $where .= " OR ({$wpdb->posts}.post_type='actor' AND " . 
              "{$wpdb->posts}.ID IN (" . 
                "SELECT post_id FROM {$wpdb->postmeta} " .
                "WHERE meta_key='mark-as-news')) ";
  return $where;
  • Won't this affect more queries than homepage alone? Seems to be generic hook. – Rarst Oct 24 '10 at 8:04
  • @Rarst - You are absolutely right, that slipped by me. I'll correct. BTW, that's the kind of thing I was talking about here: core.trac.wordpress.org/ticket/15063 – MikeSchinkel Oct 24 '10 at 8:54
  • Thanks very much, Mike. And thanks for making my question make more sense. Please forgive my stupidity, but I have no idea what to do after adding this code to my functions.php file. – webcodeslinger Oct 24 '10 at 16:41
  • As far as I can tell this is also the answer to "What is the best way to modify the default WP_Query without affecting subsequent post queries. $query===$wp_the_query is something I didn't realize was possible :) – jerclarke Nov 11 '10 at 22:38
  • @Jeremy Clarke - @Denis-de-Bernardy suggested $query===$wp_the_query to me on trac recently so props go to him. – MikeSchinkel Nov 11 '10 at 23:41

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.

  • Thanks, Rarst. In your opinion, given that two of these custom post types need custom fields, would this work better if they were categories and not custom post types? Thanks! – webcodeslinger Oct 23 '10 at 21:30
  • @webcodeslinger - For what you want you'd be adding complexity to the SQL required if you were to use categories and you'd loose all the benefits of custom post types. – MikeSchinkel Oct 24 '10 at 3:54
  • @MikeSchinkel - Thanks, that's what I was thinking. – webcodeslinger Oct 24 '10 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.