[wp-hackers] pre_get_posts problem

Mike Schinkel mikeschinkel at newclarity.net
Mon Aug 9 04:03:09 UTC 2010


On Aug 8, 2010, at 9:10 PM, Krusty Ruffle wrote:
> 
> I'm trying to add a custom post type on the main index, author index, and
> date indexes. This doesn't seem to work on the main index.
> 
> The only thing I've been able to get to work right is 'pre_get_posts' which
> is supposed to be the wrong way to do it. I am lost and growing frustrated,
> I understand why 'pre_get_posts' is wrong but have been unable to get any of
> the suggested alternatives to work...


Have you tried adding an is_home() to your if() and using your own preferred array of post types?  (If you'll read through the code I would hope it's reasonably obvious that it would only work for author and search pages.)

Try:

add_filter( 'parse_query', 'my_parse_query' );
function my_parse_query( $query ) {
      $q = $query->query;
      $is_menu = (isset($q['post_type']) && $q['post_type']=='nav_menu_item');
      if (!$is_menu && is_home() || ((is_author() && isset($q['author_name']) || (is_search() && isset($q['s']))))) {
              $query->query_vars['post_type'] = array( 'post','lesson', 'howto', 'workshop', 'attachment' );
      }
}

HTH.

-Mike


More information about the wp-hackers mailing list