Advertisements

Include custom post types in default queries

When working with custom post types you might want to include them in particular result sets such as tag pages or category pages.
All you need to do in order to achieve this is hooking a little function in the parse_query action.

function query_my_post_types( &$query ) {
	// Do this for all category and tag pages, can be extended to is_search() or is_home() ...
	if ( is_category() || is_tag() ) {
		$post_type = $query->get( 'post_type' );
		// ... if no post_type was defined in query then set the defaults ...
		if ( empty( $post_type ) ) {
			$query->set( 'post_type', array(
					'post',
					'custom-post-type1',  // your custom post type
					'custom-post-type2',  // an other custom post type
				) );
		}
	}
}
add_action( 'parse_query', 'query_my_post_types' );
Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: