How to show Multiple Custom Post Types that share Custom Taxonomies

This is a sample query where the home page would show all custom post types that shared the custom taxonomy, “Front Page”

First, we set up our Custom Post Types in the theme’s function.php file. We will create post types called, “Blog”, “Biology”, “Biodiversity”, and “Filmmaking”.

 _x('Biodiversity', 'post type general name'),
    'singular_name' => _x('Biodiversity', 'post type singular name'),
    'add_new' => _x('Add New', 'banner'),
    'add_new_item' => __('Add New Biodiversity Item'),
    'edit_item' => __('Edit Biodiversity Item'),
    'new_item' => __('New Biodiversity Item'),
    'view_item' => __('View Biodiversity Item'),
    'search_items' => __('Search Biodiversity'),
    'not_found' =>  __('None found'),
    'not_found_in_trash' => __('None found in Trash'), 
    'parent_item_colon' => '',
    'menu_name' => 'Biodiversity'
  );
  $args = array(
	'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'has_archive' => true, 
    'hierarchical' => false,
    'menu_position' => 20,
    'supports' => array('title','editor','custom-fields','thumbnail','page-attributes'),
	'taxonomies' => array('category', 'post_tag', 'biodiversity') 
  ); 
  register_post_type('biodiversity',$args);  

/* Custom Post Type How to filmmaking */
  $labels = array(
	'name' => _x('Filmmaking Post', 'post type general name'),
    'singular_name' => _x('Filmmaking Post', 'post type singular name'),
    'add_new' => _x('Add New', 'Filmmaking Post'),
    'add_new_item' => __('Add New Filmmaking Post Item'),
    'edit_item' => __('Edit Filmmaking Post Item'),
    'new_item' => __('New Filmmaking Post Item'),
    'view_item' => __('View Filmmaking Post Item'),
    'search_items' => __('Search Filmmaking Post'),
    'not_found' =>  __('None found'),
    'not_found_in_trash' => __('None found in Trash'), 
    'parent_item_colon' => '',
    'menu_name' => 'Filmmaking'
  );
  $args = array(
	'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'has_archive' => true, 
    'hierarchical' => false,
    'menu_position' => 20,
    'supports' => array('title','editor','custom-fields','thumbnail','page-attributes'),
	'with_front' => true, 
	'taxonomies' => array('post_tag', 'blog') 
  ); 
  register_post_type('filmmaking',$args); 
  
/* Custom Post Type Blog */
  $labels = array(
	'name' => _x('Blog Post', 'post type general name'),
    'singular_name' => _x('Blog Post', 'post type singular name'),
    'add_new' => _x('Add New', 'Blog Post'),
    'add_new_item' => __('Add New Blog Post Item'),
    'edit_item' => __('Edit Blog Post Item'),
    'new_item' => __('New Blog Post Item'),
    'view_item' => __('View Blog Post Item'),
    'search_items' => __('Search Blog Post'),
    'not_found' =>  __('None found'),
    'not_found_in_trash' => __('None found in Trash'), 
    'parent_item_colon' => '',
    'menu_name' => 'Blog Post'
  );
  $args = array(
	'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'has_archive' => true, 
    'hierarchical' => false,
    'menu_position' => 20,
    'supports' => array('title','editor','custom-fields','thumbnail','page-attributes'),
	'taxonomies' => array('post_tag', 'blog') 
  ); 
  register_post_type('blog',$args);  

/* Custom Post Type Biology */
  $labels = array(
	'name' => _x('Biology', 'post type general name'),
    'singular_name' => _x('Biology', 'post type singular name'),
    'add_new' => _x('Add New', 'banner'),
    'add_new_item' => __('Add New Biology Item'),
    'edit_item' => __('Edit Biology Item'),
    'new_item' => __('New Biology Item'),
    'view_item' => __('View Biology Item'),
    'search_items' => __('Search Biology'),
    'not_found' =>  __('None found'),
    'not_found_in_trash' => __('None found in Trash'), 
    'parent_item_colon' => '',
    'menu_name' => 'Biology'
  );
  $args = array(
	'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'has_archive' => true, 
    'hierarchical' => false,
    'menu_position' => 20,
    'supports' => array('title','editor','custom-fields','thumbnail','page-attributes'),
	'taxonomies' => array('post_tag', 'biology') 
  ); 
  register_post_type('biology',$args);
    
} // End Custom Post Types
?>

Next we build our Custom Taxonomy called, “Front Page” that is shared by the four Custom Post Types. That way we can query all post types that have the custom tax, front page checked and show them on the front page of the website. Place the Custom Taxonomy after your Post Types function in your theme’s function.php file.

// WP Menu Categories
add_action( 'init', 'build_taxonomies', 0 );
 
function build_taxonomies() {
// For showing on Front Page	
register_taxonomy( 'front_page',
      array( 
	  'blog','biodiversity','biology', 'filmmaking' ), // List Custom Post Types to show
      array( 
	  'hierarchical' => true,
      'label' => 'Show on Front Page?',               
      'query_var' => true,
	  'show_admin_column' => true,
      )
   );

} // End build taxonomies

After building the Custom Taxonomy, we go and create one. You create it just like you create normal categories. Click on “Show on Front Page?”, now listed under each Custom Post Type, and create a new one called, “Front Page” and make sure it’s slug ends up being, “front-page”.
Creating a Custom Taxonomy
Custom Taxonomy Created

Now that we have all that created we can publish some posts. As a test go create one from each new custom post type and make sure to check the, “Show on Front Page” custom taxonomy that we created. Once we’ve done that we can query our posts like below:

    array('blog','biodiversity', 'filmmaking', 'biology'), //You list of Custom Post Types 'posts_per_page' => '100', // # of posts to show 'tax_query' => array( //Custom Taxonomy "front_page" array( // array within an array 'taxonomy' => 'front_page', // Name when registering CT 'field' => 'slug', 'terms' => 'front-page' // slug created by WP when you make your entry ) ) ); $query = new WP_Query( $args ); // Query all of your arguments from above ?> have_posts() ) : $query->the_post(); // the loop ?>
  • ">

Leave a Reply

Your email address will not be published. Required fields are marked *

50 − = 42