My Custom Structure is set to "%postname%/"

This works as expected, but for Posts with the Category "Latest News", I would like to change the URL structure to be "%postid%/".

I put the following code in my functions.php:

    add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
    // Get the category for the post
    $category = get_the_category($post->ID);
    if (  !empty($category) && $category[0]->cat_name == "latestnews" ) {
        $cat_name = strtolower($category[0]->cat_name);
        $permalink = trailingslashit( home_url('/'. $cat_name . '/' . $post->ID .'/' ) );
    return $permalink;
add_action( 'init', 'custom_rewrite_rules' );
function custom_rewrite_rules() {
        'top' // The rule position; either 'top' or 'bottom' (default).

This does ngenerates the desired URL, "myurl.com/POSTID" when I try to click on the desired Post its shows me 404 error

I'd like to accomplish the URL rewrite without needing to register a new Custom Post Type.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.