Leave a Reply

2 comments

  1. 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:

    add_action('posts_where','yoursite_posts_where',10,2);
    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;
    }
    
  2. 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.