Question

I seem to have hit a brick wall here. We are building a new wordpress website and the permalink structure is '/labs/%postname%/'. This works fine for all posts, using /labs/ as the base. Our issue is that we need a custom post type for our portfolio, where my base url should really be /work, then probably /work/digital, /work/cgi and so on. I created the custom post type and the taxonomy as well, but no matter what i do, the url for all my custom posts begin with /labs/.

I tried loads of suggestions, from rewriting rules to changing the rewrite parameter in the register_taxonomy function. The code i am using is below:

function create_post_type() {
register_post_type( 'our_work',
    array(
        'labels' => array(
            'name' => __( 'Work Items' ),
            'singular_name' => __( 'Work Item' )
        ),
        'public' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'work'),
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'custom-fields',
            'revisions',
            'thumbnail',
            'page-attributes'   
        ),
    )
);
}
add_action( 'init', 'create_post_type' );

function create_taxonomies() {
register_taxonomy('work-categories', array('our_work'), array(
    'labels' => array(
        'name' => 'Work Categories'
    ),
    'show_ui' => true,
    'show_tagcloud' => false,
    'rewrite' => true,
    'hierarchical' => true,
    'with_front' => false
));
}
add_action('init', 'create_taxonomies');

Have tried to change the rewrite parameter to true, false, hierarchical and even replace it with / or the name of the custom post type..

Is it even possible to do that? All we essentially want to do here is have the blog and custom posts sit under 2 different base paths, i would think this would be easy enough to do!

sorry if this was covered before, the terminology is mixed up and search results bring up anything really remote to this matter.

Thanks!

Was it helpful?

Solution

I always seem to find my own solution minutes after posting here... here goes, for people having the same issue, make sure the slug in both the registration of the custom post type and the taxonomy is the same... Also make sure you use the generator, as it is easy to miss something... So now i am getting both bases working all right no issues at all! :)

Edit: So, whatever base you use in your general settings will work for all your normal posts but with the added function and tying everything right, all custom ones will be rewritten. Hope this helps!

<?php 

/**
 * Register Custom Post Type
*/
function create_post_type() {
register_post_type( 'our_work', array(
        'label'              => 'Work Items',
        'singular_label'     => 'Work Item',
        'description'        => 'Our work items',
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'query_var'          => true,
        'has_archive'        => true,
        'hierarchical'       => true,
        'supports'           => array( 'title', 'editor', 'excerpt', 'custom-fields', 'revisions', 'thumbnail', 'page-attributes' ),
        'rewrite'            => array( 'slug' => 'work-items/%work-categories%', 'with_front' => false),
    )
);
}
add_action( 'init', 'create_post_type' );

/**
 * Register Custom Taxonomy
*/
function create_taxonomies() {
$labels = array(
    'name'                       => 'work-categories',
    'singular_name'              => 'work-category',
    'menu_name'                  => 'Work Categories',
    'all_items'                  => 'All Items',
    'parent_item'                => 'Parent Item',
    'parent_item_colon'          => 'Parent Item:',
    'new_item_name'              => 'New Item Name',
    'add_new_item'               => 'Add New Item',
    'edit_item'                  => 'Edit Item',
    'update_item'                => 'Update Item',
    'separate_items_with_commas' => 'Separate items with commas',
    'search_items'               => 'Search Items',
    'add_or_remove_items'        => 'Add or remove items',
    'choose_from_most_used'      => 'Choose from the most used items',
    'not_found'                  => 'Not Found',
);
$rewrite = array(
    'slug'                       => 'work-items',
    'with_front'                 => false,
    'hierarchical'               => true,
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => false,
    'show_tagcloud'              => false,
    'rewrite'                    => $rewrite,
    'query_var'                  => 'work-categories',
);
register_taxonomy( 'work-categories', 'our_work', $args );
}
add_action('init', 'create_taxonomies');


/**
 * Add custom filters to replace categories in custom taxonomy paths
*/
add_filter('post_link', 'brand_permalink', 1, 3);
add_filter('post_type_link', 'brand_permalink', 1, 3);

function brand_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%work-categories%') === FALSE) return $permalink;

// Get post
$post = get_post($post_id);
if (!$post) return $permalink;

// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, 'work-categories');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
    $taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = 'uncategorised';

return str_replace('%work-categories%', $taxonomy_slug, $permalink);
}

?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top