2

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..

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?

  • make sure you have 'has_archive' => true when you're registering your post type. – Султон Атхам Aug 20 at 18:37
  • Has this ever been solved? By one of the answers (then accept) or in another way (then describe and accept yourself). I'm having the same issue, there is various same/similar on StackExchange/StackOverflow and elsewhere online, none of those suggestions have helped... – user3445853 Oct 28 at 20:38
6

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

  • Yep tried that, still nothing – fightstarr20 Jul 23 '13 at 14:03
  • FFS - Keep forgetting that! – Stender Mar 12 at 13:52
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.

1

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.

  • And does that pre_get_posts addition solve the problem? If not, this is not an answer and should not have been posts as one. – s_ha_dum Aug 14 '13 at 0:54
0

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.

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.