• I write the following codes to change the permalinks of posts with some specific categories, as below:

    /* 2024-12-04: Define custom permalinks for some specific posts */
    function post_custom_permalink($permalink, $post) {
        // Check if post belongs to some specific categories
        if (has_category('case-studies', $post)) {
            $permalink = home_url('case-studies/' . $post->post_name . '/');
            return $permalink;
        }
        else if (has_category('guides', $post)) {
            $permalink = home_url('guides/' . $post->post_name . '/');
            return $permalink;
        }
        else if (has_category('tools', $post)) {
            $permalink = home_url('tools/' . $post->post_name . '/');
            return $permalink;
        }    
    
        // For all other cases, return the original permalink
        return $permalink;
    }
    add_filter('post_link', 'post_custom_permalink', 10, 2);

    Now the problem is: when I create a post “Test Tool” with category tools, its permalink is shown as https://www.sample.com/tools/test-tool/.

    But when I visit https://www.sample.com/tools/test-tool/, I will get 404 not found error. If I visit https://www.sample.com/test-tool/, I can see the post. Why?

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘WordPress post_link produces permalink that cannot be visited’ is closed to new replies.