0
votes

I've created a custom taxonomy taxonomy in a plugin for my wordpress theme. After the plugin is activated and I navigate to the admin section for the custom post type all posts and pages from the site show. Also when I try to delete these posts and pages from the custom post type admin area I get an 'invalid post type' error. Has anyone had any experience of this happening and is there a solution?

'code'add_action ('init', 'create_post_type' );

function create_post_type() {

register_post_type( 'my_slide',

    array(

        'labels' => array(
            'name' => __( 'Slides' ),
            'singular_name' => __( 'Slide' ),
            'add_new' => 'Add New',
            'add_new_item' => 'Add New Slide',
            'edit' => 'Edit',
            'edit_item' => 'Edit Slide',
            'new_item' => 'New Slide',
            'view' => 'View',
            'view_item' => 'View Slide',
            'search_items' => 'Search Slides',
            'not_found' => 'No Slides found',
            'not_found_in_trash' => 'No Slides found in Trash',
            'parent' => 'Parent Slide'
        ),

    'public' => true,
    'has_archive' => true,
    'show_ui' => true,  
    'capability_type' => 'post',  
    'hierarchical' => false,  
    'rewrite' => true,
    'map_meta_cap' => true,
    'query_var' => false,
    'register_meta_box_cb' => 'slide_meta_box_add',
    'supports' => array('title', 'editor', 'thumbnail', 'post-formats', 'Custom            Featured Image links')

    )
);



}

add_action( 'init', 'create_slider_taxonomies', 0 );

function create_slider_taxonomies() {
register_taxonomy(
    'slider_category',
array( 'my_slide' ),
    array(
        'labels' => array(
            'name' => 'Slide Category',
            'add_new_item' => 'Add New Slide Category',
            'new_item_name' => 'New Slide Category Name'
        ),
        'show_ui' => true,
        'show_tagcloud' => false,
    'show_admin_column' => true,
        'hierarchical' => true
    )
);

}'code'
1
Show your plugin code. - brasofilo
@brasofilo - I've been over and over why it could be behaving like this and there is no mention on any forum I've googled. I'm beginning to think the problem could be something to do with the server or database unless of course you have any other suggestions. - Matthew Lillistone
No, there's nothing wrong with that code. Disable all other plugins and swap to a default theme, does it still happens? - brasofilo
It's the theme which is doing it. Is this a query related problem do you think? - Matthew Lillistone
@Brasofilo Solved - pre_get_posts filter - Matthew Lillistone

1 Answers

1
votes

As you stated, the problem was with the pre_get_posts() not checking if the admin area is being displayed. This can be tested for using theis_admin() condition.

function add_post_types_to_query( $query ) {
    if ( (!is_admin()) && $query->is_main_query() )
        $query->set( 'post_type', array( 'post', 'video' ) ); //video is a custom post type
    return $query;
}
add_action( 'pre_get_posts', 'add_post_types_to_query' );

The above will stop the posts showing up in all admin areas.

There are another couple of conditions you may wish check for. With the above I had an issue with links to pages stoping working. This is solved by checking to see if the posts page is_archive(). With this in place custom post types may not show up on the home page. This can be solved by adding the is_home() condition to the function.

A final pre_get_posts function may look like.

function add_post_types_to_query( $query ) {
    if ( (!is_admin()) && $query->is_main_query() )
        if ( $query->is_archive() || $query-> is_home() ) { 
            $query->set( 'post_type', array( 'post', 'video' ) ); //video is a custom post type
        }
    return $query;
}
add_action( 'pre_get_posts', 'add_post_types_to_query' );

I hope this helps anyone looking at this post in the future.