Short version
Within your loop call: get_template_part( 'loop', get_post_type() );
You need now a loop.php
as general fallback template (the default view).
You can generate as many post_type specific templates as you need, e.g. loop-newpost.php
and loop-book.php
.
But you are overthinking it a bit and adding another query on top the main query (page load?). You can add full template support to your theme with the following.
A better apporach
Add your CPTs.
function add_my_custom_post_type() {
$args = array(
'menu_position' => 8,
'menu_icon' => 'dashicons-admin-post',
'public' => true,
'publicly_queryable' => true,
'query_var' => false,
'capability_type' => 'post',
'has_archive' => true,
'rewrite' => array(
'slug' => 'new-post',
'feeds' => true,
'pages' => true,
'with_front' => true,
),
'taxonomies' => array(
'author'
),
'supports' => array(
'title',
'editor',
'thumbnail',
'excerpt'
)
);
register_post_type( 'newpost', $args );
$args['labels']['name'] = __('Books', 'my_textdomain');
$args['labels']['all_items'] = __('All Books', 'my_textdomain');
$args['labels']['singular_name'] = __('Book', 'my_textdomain');
$args['rewrite']['slug'] = 'book';
register_post_type( 'book', $args );
}
add_action('init', 'add_my_custom_post_type');
Add your custom taxonomy.
Attention: author is a slug used by WordPress, so you need to be creative: tax_author
for the taxonomy name & query_var
and the-author
for the slug.
function add_my_custom_taxonomy() {
register_taxonomy(
'tax_author',
array(
'newpost',
'book'
),
array(
'hierarchical' => false,
'labels' => array(
'name' => __( 'Authors', 'my_textdomain' ),
'singular_name' => __( 'Author', 'my_textdomain' ),
'menu_name' => __( 'Author', 'my_textdomain' ),
),
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array(
'slug' => 'the-author',
'with_front' => true,
'hierarchical' => false
)
)
);
}
add_action('init', 'add_my_custom_taxonomy');
Add the CPT to the main queries in questions with the pre_get_posts
hook
function add_my_custom_post_type_to_query( $query ) {
if ( $query->is_main_query() && $query->is_home() && !is_admin() /* No filtering of CPTs in the backend */) {
$query->set( 'post_type', array('newpost', 'book') );
}
}
add_action( 'pre_get_posts', 'add_my_custom_post_type_to_query' );
Edit: You don’t need this for taxonomies, but if you want to add your CPT to the main query, use this hook. — Thanks to @Pieter Goosen for the clarification.
Within your taxonomy-tax_author.php
template file you can now use the main loop:
if (have_posts()) while (have_posts()):
the_post();
get_template_part( 'loop', get_post_type() );
You need now a loop.php
as general fallback template (the default view).
You can generate as many post_type specific templates as you need, e.g. loop-newpost.php
and loop-book.php
.
Update:
If you want to order the query by post_type you need to hook into the pre_get_posts
again
function order_my_custom_taxonomy( $query ) {
if ( $query->is_main_query() && $query->is_tax('tax_author') && !is_admin() /* No filtering of CPTs in the backend */) {
$query->set( 'orderby', 'type' );
$query->set( 'order', 'ASC' );
}
}
add_action( 'pre_get_posts', 'add_my_custom_post_type_to_query' );
For detailed information about how to filter and order follow the provided links.
More information about:
- Post Type: Custom Post Types
- Taxonomy: Custom Taxonomies
- Action Hook: pre_get_posts
- Loading Templates: wptherightway.org
- Order By: type