You can try something like this:
add_action( 'init', 'so6159452_init' );
function so6159452_init()
{
add_rewrite_tag( '%category_id%', '([0-9]+)' );
}
add_filter( 'post_link', 'so6159452_post_link', 10, 2 );
function so6159452_post_link( $permalink, $post )
{
if ( false !== strpos( $permalink, '%category_id%' ) ) {
$cats = get_the_category( $post->ID );
if ( $cats ) {
usort( $cats, '_usort_terms_by_ID' ); // order by ID
$category_id = $cats[0]->cat_ID;
} else {
// Error: no category assigned to this post
// Just use a dummy variable
$category_id = '0';
}
$permalink = str_replace( '%category_id%', $category_id, $permalink );
}
return $permalink;
}
Please note this code is not mine. It has been taken from this stackoverflow answer.
Giving credit where it is due.
o if you find this useful, go to the above SO link and upvote the answer
Hi,
Try out this plugin https://wordpress.org/plugins/custom-permalinks/. Hope it will help you sure and let me know the feedback.
Thread Starter
smck87
(@smck87)
Thanks for answers, but I decided to use built in solution and stopped at default categories permalinks.
You can follow the previous reply of @shariqkhan2012. It should help to you.