[wp-hackers] pre_get_posts problem

Andrew Nacin wp at andrewnacin.com
Wed Jul 21 05:14:43 UTC 2010

On Wed, Jul 21, 2010 at 12:57 AM, Angelia Baladon
<angelia at 10sexyapples.com>wrote:

> Howdy all,
> I'm running a pre_get_posts filter on is_author() in order to include my
> custom post types when author.php is called.
> If I don't include nav_menu_item in the query set, the primary menu (
> basically the default twenty ten nav ) disappears. This makes sense.
> But, if I DO include nav_menu_item in the query set, I get nav_menu_item/s
> listed in the loop.
> I could have a bug related to some of my attempts to get the link object
> type, but, wanted to know if anyone else has experienced this before I wipe
> the database.
> If anyone is using the new navigation and could confirm if it's just me
> that'd be swell:
> add_filter( 'pre_get_posts', 'custom_posts' );
> function custom_posts( $query ) {
>    if ( is_author() )
>        $query->set( 'post_type', array( 'post', 'nav_menu_item', 'lesson',
> 'howto', 'workshop', 'attachment' ) );
>    return $query;
> }
> replace my custom post types with yours and check your author page.

This is one of the dangers/pitfalls of using pre_get_posts. It seems to be
recommended often for some reason when dealing with custom post types, but
it is much better to modify the request itself, versus any old get_posts or
WP_Query call.

The correct filter here would be either 'request' (direct QV access) or
parse_request (whole WP object to play with).

More information about the wp-hackers mailing list