bradtrivers

Add custom post type to category loops

Mar 9th, 2012
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.83 KB | None | 0 0
  1. //Add Resource custom post type to default query for category archive
  2. add_filter( 'pre_get_posts', 'add_resource_post_type'  );
  3. function add_resource_post_type( $query ) {
  4.     if ( is_category() ||  !is_admin() && $query->is_category_page ) {
  5.         if(!is_array($query->query_vars['post_type'])) {
  6.           $postTypes = array($query->query_vars['post_type']);
  7.         } else {
  8.           $postTypes = $query->query_vars['post_type'];
  9.         }
  10.         if(!in_array('resources',$postTypes)) {
  11.           if(count($postTypes) == 1 && trim($postTypes[0]) == "") {
  12.             $postTypes[] = 'post';
  13.             $postTypes[] = 'resources';
  14.           } else {
  15.             $postTypes[] = 'resources';
  16.           }
  17.         }
  18.  
  19.         $query->query_vars['post_type'] = $postTypes;
  20. //         print_r($postTypes);
  21.     }
  22.     return $query;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment