fourwhitesocks

Add Custom Post Type to Tags and Categories

Feb 24th, 2016
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.56 KB | None | 0 0
  1. //add custom post type to tags and categories
  2. function add_custom_types_to_tax( $query ) {
  3.     // Return right away if in admin or not working on the main frontend query
  4.     if ( is_admin() || ! $query->is_main_query() ) {
  5.         return;
  6.     }
  7.  
  8.     // Check if we're on a category or tag archive. If so, add our post type
  9.     if( ( $query->is_category() || $query->is_tag() ) ) {
  10.         // Get all your post types
  11.         $post_types = array('post' , 'custom-post-type-goes-here');
  12.         $query->set( 'post_type', $post_types );
  13.     }
  14. }
  15. add_action( 'pre_get_posts', 'add_custom_types_to_tax' );
Advertisement
Add Comment
Please, Sign In to add comment