Search code examples
phpwordpresspermalinks

WordPress || Custom post types, permalinks & numbers


This is for a cat shelter. I have a custom post type, called "cat".

I then have a taxonomy for availability, with "available" and "homed".

Everything works fine... until there are duplicate names of cats. For example, if Milo comes in one month, then a year later, another Milo comes in. Obviously, WordPress will create siteurl/cat/milo/, and then for the second, create siteurl/cat/milo-2/.

Original Milo works fine... as soon as the number is put in, milo-2 doesn't work, and redirects to a 404.

Custom Post Type

function cat_post_type() {

    $labels = array(
        'name'                => _x( 'Cats', 'Post Type General Name', 'text_domain' ),
        'singular_name'       => _x( 'Cat', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'           => __( 'Cats', 'text_domain' ),
        'name_admin_bar'      => __( 'Cats', 'text_domain' ),
        'parent_item_colon'   => __( 'Parent Cat:', 'text_domain' ),
        'all_items'           => __( 'All Cats', 'text_domain' ),
        'add_new_item'        => __( 'Add Cat', 'text_domain' ),
        'add_new'             => __( 'New Cat', 'text_domain' ),
        'new_item'            => __( 'New Cat', 'text_domain' ),
        'edit_item'           => __( 'Edit Cat', 'text_domain' ),
        'update_item'         => __( 'Update Cat', 'text_domain' ),
        'view_item'           => __( 'View Cat', 'text_domain' ),
        'search_items'        => __( 'Search cats', 'text_domain' ),
        'not_found'           => __( 'No cats found', 'text_domain' ),
        'not_found_in_trash'  => __( 'No cats found in Trash', 'text_domain' ),
    );
    $args = array(
        'label'               => __( 'cats', 'text_domain' ),
        'description'         => __( 'All cats', 'text_domain' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'thumbnail', 'comments'),
        'taxonomies'          => array( 'available', 'homed' ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'menu_position'       => 5,
        'menu_icon'           => 'dashicons-smiley',
        'show_in_admin_bar'   => true,
        'show_in_nav_menus'   => true,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
    );
    register_post_type( 'cat', $args );

}

Taxonomy

// Hook into the 'init' action
add_action( 'init', 'cat_post_type', 0 );


    function add_custom_taxonomies() {
      // Add new "Locations" taxonomy to Posts
      register_taxonomy('availability', 'cat', array(
        // Hierarchical taxonomy (like categories)
        'hierarchical' => true,
        // This array of options controls the labels displayed in the WordPress Admin UI
        'labels' => array(
          'name' => _x( 'Cats Availability', 'taxonomy general name' ),
          'singular_name' => _x( 'Cat Availability', 'taxonomy singular name' ),
          'search_items' =>  __( 'Search Cat Availability' ),
          'all_items' => __( 'All Availability' ),
          'parent_item' => __( 'Parent Availability' ),
          'parent_item_colon' => __( 'Parent Availability:' ),
          'edit_item' => __( 'Edit Availability' ),
          'update_item' => __( 'Update Availability' ),
          'add_new_item' => __( 'Add New Availability' ),
          'new_item_name' => __( 'New Availability' ),
          'menu_name' => __( 'Availability' ),
        ),
        // Control the slugs used for this taxonomy
        'rewrite' => array(
          'slug' => 'cats', // This controls the base slug that will display before each term
          'with_front' => false, // Don't display the category base before "/locations/"
          'hierarchical' => true // This will allow URL's like "/locations/boston/cambridge/"
        ),
      ));
    }
    add_action( 'init', 'add_custom_taxonomies', 0 );

We thought it might be something in the DB conflicting... tried re-saving permalinks, removing unused DB entries, disabling all plugins... nothing.

Manually renaming the cat to, say, milo_ works fine, but with any number in it just sends back my 404.

Using Starkers responsive theme, no child theme. Standard htaccess.

Am I missing something blatantly obvious?

Unfinished site here - find a cat with a number in its link... http://catsnottingham.zellement.com/cats/homed/


Solution

  • I have fixed this - I think it must have been a database conflict, corruption or error, so I started again with a fresh WordPress install, which has fixed the issue.

    Part of me thinks using "cat" as a CPT may have conflicted with categories... somehow.