I want to customize the posts category and single permalinks. I'm doing something like this:
add_filter('post_link', 'set_post_links' , 10, 2);
function set_post_links($permalink, $post){
if(is_wp_error($post) || empty($post->post_name)) return $permalink;
return home_url(user_trailingslashit("news/$post->post_name"));
}
add_filter('category_link', 'lr_filter_category_link', 10, 2 );
function lr_filter_category_link( $termlink, $term_term_id ) {
$term = get_term($term_term_id);
return home_url(user_trailingslashit("news/$term->slug"));
};
Generated urls/links are correct, but when clicked they go to a 404 page. I already refreshed the permalinks from admin.
How to solve?
the_permalink
just before output. Unless you add a rewrite rule, no permalinks have actually been changed. You can achieve most of what you're trying to do from the permalinks settings page anyway without writing any code – Tom J Nowell♦ Dec 3 '20 at 17:33