I have the following permalink structure:
/%category%/%year%/%monthnum%/%postname%
I'm using the code I found here to get rid of the "uncategorized" slug when I don't set any category.
URLs are generated fine but when I visit them I get 404. Seems like I'm missing some rewrite or similar?
I'm on WordPress 4.3.1 and the code I placed in my functions.php
is:
function my_pre_post_link( $permalink, $post, $leavename ) {
if( $post->post_type != 'post' ) return $permalink;
$cats = get_the_category($post->ID);
if( ! count($cats) ) return $permalink;
usort($cats, '_usort_terms_by_ID');
$category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
$category_object = get_term( $category_object, 'category' );
return _clear_uncategorized($category_object, $permalink);
}
function _clear_uncategorized($cat, $permalink) {
if( $cat->slug == 'uncategorized' ) {
return str_replace('%category%/', '', $permalink);
}
$parent = $cat->parent;
if ( !$parent )
return $permalink;
return _clear_uncategorized($parent, $permalink);
}
add_filter( 'pre_post_link', 'my_pre_post_link', 9, 3 );