0

I have two Post Type('newpost' and 'book') associate with custom taxonomy(newcategory)(writer1, writer2, writer3...)

I have need to show each custom post at separate style with Post term.

My code is :

 $post_type = array('newpost','book');
 //echo get_query_var('term');

     foreach($post_type as $postType)
     {
 $args = array(
    'post_type' => $postType,
    'tax_query' => array(
                array(
                    'taxonomy' => $taxonomy,
                    'field' => 'slug',
                    'terms' => rawurldecode(get_query_var('term'))
                )
            )
    );

 $my_query = new WP_Query( $args );
 if($my_query->have_posts()) :
     while ($my_query->have_posts()) : $my_query->the_post();
     {

//I got all the post here by //echo '<h1>'.the_title().'</h1>'; but i have need it separately.          
//Here I want to display all the term of the custom post separately  

      endwhile;
 endif;


     }
0

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:

3
  • 1
    post_type is set to any on taxonomy archives, so you do not need to include custom post types on taxonomy archives, it is done by default ;-) – Pieter Goosen Mar 14 '16 at 19:01
  • if (have_posts()) while (have_posts()): the_post(); get_template_part( 'loop', get_post_type() ); //At there if I want to show post_type='book' first and after it want to show post_type='newpost' type how can control the loop. And how get the post custom fields created by using plugin. – vj tester Mar 15 '16 at 6:33
  • updated (thx to @PieterGoosen). vj tester i added your request, but please read about pre_get_posts at the codex to get other ways to manipulate the main query. – iantsch Mar 15 '16 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.