[wp-hackers] pre_get_posts problem

Angelia Baladon angelia at 10sexyapples.com
Tue Aug 3 22:26:30 UTC 2010


I see that Justin Tadlock has updated his post regarding the use of
pre_get_posts and the code is now:

add_filter( 'pre_get_posts', 'customtype_get_posts' );

function customtype_get_posts( $query ) {

    if ( is_author() && false == $query->query_vars['suppress_filters'] )
        $query->set( 'post_type', array( 'post', 'howto', 'lesson' ,
'attachment' ) );

    return $query;
}

I tested this and it appears to work for my use case where the previous code
failed, but, I'm still weary, with all the discussion of how wrong the
pre_get_posts approach is, on whether to use it.

Mike submitted this approach ... thanks again Mike ... but, of course, as
noted by Mike, it has a few limitations.

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_author() && isset($q['author_name'])))) {
               $query->query_vars['post_type'] = array( 'post', 'lesson',
'howto', 'attachment' );
       }
}

I'm assuming that another variation of Mike's version, minus the
limitations, would be the most appropriate, but, would like to try to really
get to the bottom of discovering the "holy grail" for getting custom post
types inserted into specific loops.

Does anyone have any feedback regarding this new version of using
pre_get_posts, and where it might fail or cause problems in other areas?

Trying to keep this topic alive until said time arrives~


More information about the wp-hackers mailing list