Custom Post Type archive giving 404

I am using WAMP with the latest version of WordPress installed. I have a custom post type created using the following…

function register_fruit() {
register_post_type('fruit', array(
'labels' => array(
'name' => __( 'Fruit' ),
),
'public' => true,
'has_archive' => true,
'capability_type' => 'post',
'rewrite' => array("slug" => "/fruit", "with_front" => false),
'supports' => array( 'title', 'editor', 'thumbnail'),
'taxonomies' => array('category', 'post_tag') // this is IMPORTANT
));
}
add_action('init', 'register_fruit', 0 );

This works fine when viewing individual items for example..

Read More
www.mydomain.com/fruit/apple
www.mydomain.com/fruit/orange
www.mydomain.com/fruit/pear

But if I try and view the archive pages such as…

www.mydomain.com/fruit

I get a 404, I can’t see where the problem lies, can anyone help?

Related posts

4 comments

  1. Have you tried refreshing permalinks?
    Settings -> Permalinks (don’t have to change anything)
    Then try again?

  2. Remove the leading slash in your rewrite argument, This:

    'rewrite' => array("slug" => "/fruit", "with_front" => false),
    

    should be:

    'rewrite' => array("slug" => "fruit", "with_front" => false),
    

    and your archive will work correctly.

  3. I had this problem and tried all the solutions (flush permalinks, various rewrites, etc) until I stumbled in the WP Codex on

    query_var
    (boolean or string) (optional) False to disable the query_var, set as string to use custom query_var instead of default which is $taxonomy, the taxonomy’s “name”. True is not seen as a valid entry and will result in 404 issues.

    Indeed, I had 'query_var' => true which caused my problem.

  4. I have the same issue using:

    function cp_init_types() {
    register_post_type( 'nursing-home',
        array(
            'labels' => array(
                'name' => __( 'Nursing Homes' ),
                'singular_name' => __( 'Nursing Home' )
            ),
            'public' => true,
            'has_archive' => true,
            /*'rewrite' => array( 'slug' => 'nursing-homes', 'with_front' => true ),
            'supports' => array( 'title', 'editor', 'custom-fields', 'thumbnail' ),
            'publicly_queryable' => true,
            'exclude_from_search'=> false,
            'taxonomies' => array('category','post_tag'),*/
        )
    );
    /*register_taxonomy('location','nursing-home',[
            'labels' => [
                'name' => __('Locations'),
                'singular_name' => __('Location')
            ],
            'public' => true
        ]);*/
    }
    
    function add_my_post_types_to_query( $query ) {
    if ( is_post_type_archive( "nursing-home" ) && $query->is_main_query() ) {
        $query->set('type',"nursing-home");
        $test = $query->get('type');
        var_dump($test);
    }
    
    return $query;
    }
    
    add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
    add_action("init", "cp_init_types");
    

    I added the “pre_get_posts” action because I was having the same problem as fightstar.

Comments are closed.