Posts tagged with custom post type

//show custom post type in archives.
function ns_set_archive_post_types($query){
    
    // We cannot use 'is_archive' here because post type archives should not be included
    $is_target_query = 
            !is_admin() && 
            $query->is_main_query() && 
            ($query->is_date || 
            $query->is_author || 
            $query->is_category || 
            $query->is_tag || 
            $query->is_tax); 
            
    if($is_target_query){
        $target_types = array('post', 'ns_book_cpt');
        $query->set('post_type', $target_types);
    }
}
add_action('pre_get_posts', 'ns_set_archive_post_types', 10, 1);

ref:https://www.rafaelcardero.com/courses/post-types-in-wordpress/l/109/