Skip to content

Instantly share code, notes, and snippets.

@boyvanamstel
Created June 30, 2011 08:52
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boyvanamstel/1055880 to your computer and use it in GitHub Desktop.
Save boyvanamstel/1055880 to your computer and use it in GitHub Desktop.
Show custom post types on tag, category and archive pages in Wordpress
<?php
/**
* Also show custom post types in tag/category/archive pages
*/
function query_post_type($query) {
if(is_category() || is_tag()) {
$post_type = get_query_var('post_type');
if($post_type) {
$post_type = $post_type;
} else {
$post_type = array('nav_menu_item','post','custom_post_type','another_custom_post_type');
}
$query->set('post_type',$post_type);
return $query;
}
}
add_filter('pre_get_posts', 'query_post_type');
?>
@nicolasfranz
Copy link

Thanks @boyvanamstel, your function works great! I hope soon Wordpress.org could fix this error inside the core :)

@racl101
Copy link

racl101 commented Nov 13, 2015

Thank you. I was starting to pull my hair after an hour and a half of looking for this bug!

You rock dude!!!

@adrianahdez
Copy link

adrianahdez commented Apr 11, 2019

You have an issue in your code that could cause a menu disappearing. You must add the 'nav_menu_item' when you do: $post_type = $post_type;. You must modify that line to this: $post_type = array( 'nav_menu_item', $post_type ); (sorry for my bad english) –

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment