I’ve looked around quite a bit and haven’t found anything exactly what I am looking for, so I figured I’d post here. This post and answer is very very close, but I’m having trouble adapting it to what I need: How to create a permalink structure with custom taxonomies and custom post types like base-name/parent-tax/child-tax/custom-post-type-name

Here’s what I’m trying to do:

Custom post type slug: business

URL Structure:

/business-listings/  (top level archive of all the custom post type posts)
/business-listings/whatever/ (archive of the custom post type posts that have the category of 'whatever')
/business-listings/whatever/the-post/ (the single custom post type post)

Every post in the post type would have only ONE category (if that makes a difference).

I’ve tried plugins like “Custom Post Type Permalinks”, but this doesn’t allow me to use the /business-listings/whatever/ as an archive (it returns a 404 even after flushing rewrite rules). As of now I’m using “Custom Post Type UI” to create the custom post type, but if it’s simpler to add them manually inside functions.php I am all for it.

1 Answer
1

I should have looked a little closer at that link posted. There was a link in that question that contained the answer (https://wordpress.stackexchange.com/a/5313/100538). Below is the code I used to get the effect I need:

///// CUSTOM POST TYPES /////

// register the new post type
register_post_type( 'business', array( 
    'labels'                 => array(
        'name'               => __( 'Businesses' ),
        'singular_name'      => __( 'Business' ),
        'add_new'            => __( 'Add New' ),
        'add_new_item'       => __( 'Create New Business' ),
        'edit'               => __( 'Edit' ),
        'edit_item'          => __( 'Edit Business' ),
        'new_item'           => __( 'New Business' ),
        'view'               => __( 'View Businesses' ),
        'view_item'          => __( 'View Business' ),
        'search_items'       => __( 'Search Businesses' ),
        'not_found'          => __( 'No businesses found' ),
        'not_found_in_trash' => __( 'No businesses found in trash' ),
        'parent'             => __( 'Parent Business' ),
    ),
    'description'           => __( 'This is where you can create new businesses on your site.' ),
    'public'                => true,
    'show_ui'               => true,
    'capability_type'       => 'post',
    'publicly_queryable'    => true,
    'exclude_from_search'   => false,
    'has_archive'           => true,
    'menu_position'         => 2,
    'menu_icon'             => 'dashicons-id',
    'hierarchical'          => true,
    '_builtin'              => false, // It's a custom post type, not built in!
    'rewrite'               => array( 'slug' => 'business-listings/%business_cat%', 'with_front' => true ),
    'query_var'             => true,
    'supports'              => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions' ),
) );


//hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_product_taxonomies', 0 );
//add_action('admin_init', 'flush_rewrite_rules');

//create two taxonomies, genres and writers for the post type "book"
function create_product_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        'name'              => _x( 'Categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Category', 'taxonomy singular name' ),
        'search_items'      =>  __( 'Search Categories' ),
        'all_items'         => __( 'All Categories' ),
        'parent_item'       => __( 'Parent Categories' ),
        'parent_item_colon' => __( 'Parent Categories:' ),
        'edit_item'         => __( 'Edit Category' ), 
        'update_item'       => __( 'Update Category' ),
        'add_new_item'      => __( 'Add New Category' ),
        'new_item_name'     => __( 'New Category Name' ),
        'menu_name'         => __( 'Category' ),
    );  

    register_taxonomy( 'business_cat', array( 'business' ), array(
        'hierarchical'  => true,
        'labels'        => $labels,
        'show_ui'       => true,
        'query_var'     => true,
        //'rewrite'     => true,
        'rewrite'       => array( 'slug' => 'business-listings', 'with_front' => true ),
    ) );

    // Add new taxonomy, NOT hierarchical (like tags)
    $labels = array(
        'name'                       => _x( 'Scents', 'taxonomy general name' ),
        'singular_name'              => _x( 'Scent', 'taxonomy singular name' ),
        'search_items'               =>  __( 'Search Scents' ),
        'popular_items'              => __( 'Popular Scents' ),
        'all_items'                  => __( 'All Scents' ),
        'parent_item'                => null,
        'parent_item_colon'          => null,
        'edit_item'                  => __( 'Edit Scent' ), 
        'update_item'                => __( 'Update Scent' ),
        'add_new_item'               => __( 'Add New Scent' ),
        'new_item_name'              => __( 'New Scent Name' ),
        'separate_items_with_commas' => __( 'Separate scents with commas' ),
        'add_or_remove_items'        => __( 'Add or remove scents' ),
        'choose_from_most_used'      => __( 'Choose from the most used scents' ),
        'menu_name'                  => __( 'Scents' ),
    ); 

    register_taxonomy( 'scent', 'business', array(
        'hierarchical'  => false,
        'labels'        => $labels,
        'show_ui'       => true,
        'query_var'     => true,
        //'rewrite'     => array( 'slug' => 'scents' ),
    ) );
}

function wpse_5308_post_type_link( $link, $post ) {
    if ( $post->post_type === 'business' ) {
        if ( $terms = get_the_terms( $post->ID, 'business_cat' ) )
            $link = str_replace( '%business_cat%', current( $terms )->slug, $link );
        else
            $link = str_replace( '%business_cat%', 'uncategorized', $link );
    }

    return $link;
}

add_filter( 'post_type_link', 'wpse_5308_post_type_link', 10, 2 );

All credit goes to TheDeadMedic’s answer, from which I pieced this together from.

Leave a Reply

Your email address will not be published. Required fields are marked *