Custom Post Types Don’t Work on archive.php, Category pages, etc.

It took me a lot of googling, but I finally found a solution to getting my WordPress custom post types to display on archive pages!  Hooray!  I know I did this before (probably numerous times), but I was too tired to go looking through old code.

Thanks to ma’ main man, Chris, for the following lovely chunk of code:

function namespace_add_custom_types( $query ) {
  if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
    $query->set( 'post_type', array(
     'post', 'nav_menu_item', 'insert-your-custom-post-type-name-here'
		));
	  return $query;
	}
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );

Just add the above code to your functions.php file in your theme, and you will be off to the races!

Posted in ,

Toby Cryns

Toby Cryns is a freelance CTO, expert WordPress consultant, and teacher.

He offers free advice to improve your freelance biz.

He also publishes small droppings every now and then to twitter.com/tobycryns and twitter.com/themightymo

Follow Toby's contributions on Github and WP.org.