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?

  • 1
    Those filters do not change the permalink of the category or post, they're an opportunity to adjust the values of functions such as 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

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.