I Am Stack
I Am Stack

Reputation: 241

wordpress custom post type archive page issue

I know there is lot of posts and solution about this. But My problem is not fixed with those solutions. Please check here once.

I've 5 different custom post type. So I use those codes in my function.php file to display custom post types in archive.php page for category & tag.

// Add custom post types to archives
function custom_post_archive($query) {
    if ($query->is_archive)
        $query->set( 'post_type', array('nav_menu_item', 'post', 'post_type_1', 'post_type_2', 'post_type_3', 'post_type_4', 'post_type_5') );
    remove_action( 'pre_get_posts', 'custom_post_archive' );
}
add_action('pre_get_posts', 'custom_post_archive');

// Add custom post types to tag
function custom_post_tag($query) {
    if ($query->is_tag)
        $query->set( 'post_type', array('nav_menu_item', 'post', 'post_type_1', 'post_type_2', 'post_type_3', 'post_type_4', 'post_type_5') );
    remove_action( 'pre_get_posts', 'custom_post_tag' );
}
add_action('pre_get_posts', 'custom_post_tag');

After use this, My archive page show category post & posts by tag perfectly. There is not other issue .. but When I open some post type archive.. example:

http://exmple.com/post_type_1/ or http://exmple.com/post_type_3/

all are display all posts from all post type!!!

Also in my dashboard post type are mess and show all posts from all post type show under posts & others 5 post type.

So I want a solution or suggestion to fixed this. Please help.

Upvotes: 1

Views: 441

Answers (1)

I Am Stack
I Am Stack

Reputation: 241

Sorry and Thanks. I fixed the Problem by write another code.

Here is the code:

function my_post_types( $query ) {
    if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
        //$post_types = get_post_types('remote_exploit', 'webapp_exploit', 'localpriesc_exploit', 'pocnddos', 'shellcode_exploit', 'securiy_papers', 'cheat_sheet' ); 
       // $query->set( 'post_type', $post_types);
       $query->set( 'post_type', array('nav_menu_item', 'post', 'remote_exploit', 'webapp_exploit', 'localpriesc_exploit', 'pocnddos', 'shellcode_exploit', 'securiy_papers', 'cheat_sheet') );
        return $query;
    }
}
add_filter( 'pre_get_posts', 'my_post_types');

Now Everything works fine.

Thanks a lot.

Upvotes: 1

Related Questions