Let's say I want to add the permalink for the URLs: site.com/section/{postname} and the 'section' matches the name of one post's category (just for one case). After having a moment for study the documentation. I added a function for the rewrite_url. In the 'functions.php' file of my template (which is Astra). For what I add at the end:

add_action( 'init', 'wpa_rewriterules' ); 

function wpa_rewriterules() 
    // The regex to match the incoming URL 
    // The resulting internal URL: `index.php` because we still use WordPress 
    'top' ); 


I have also read that I would have to add a filter to 'the_permalink' in the same way, so that each time a link has to be displayed it will be transformed to the desired URL. So I added the code for that filter also in the same 'functions.php' file:

add_filter('the_permalink', 'post_permalink_w_seccion'); 

function post_permalink_w_seccion( $link ) { 
    global $post; 
    $postcat = get_the_category( $post->ID ); 
    if ( $post->post_type === 'post' && $postcat->slug == 'section') { 

        $link = str_replace( $post->post_name, 'section/' . $post->post_name,  get_permalink( $post ) ); 
    return $link;  

but this is what does not work for me. The urls do not transform or change. Can you help me to see what I'm doing wrong or what I'm missing?

Also, I'm not sure if this is the best way to do what I want. I'm new to the Wordpress world.

P.S. I have always saved changes to the 'permalinks' doing a flush of the rules of URLs.

Thank you.

  • 1
    You can include the category in the permalink without any code, just add %category% to your permalink structure in Settings > Permalinks. Jul 12 '19 at 9:48
  • I know. But what I need is not for all cases. so you cannot configurate it selecting the option %categoryname%%postname% from Settings > Permalinks. I would need to know what I have wrong in the filter for construction of the links works fine and shows the prefix 'section' added to them.
    – MikeBau
    Jul 12 '19 at 14:02

Function get_the_category() returns array, that is why the condition $postcat->slug == 'section' is always false.

In its current form, your filter can change links to posts that do not have the section category. You should change the function to check the post for which get_permalink() was called, instead of checking the global $post.

add_filter( 'post_link', 'post_permalink_w_section', 10, 2 ); 

function post_permalink_w_section( $link, $post )
    if ( $post->post_type === 'post' && has_category('section', $post) )
        $link = str_replace( $post->post_name, 'section/' . $post->post_name, $link ); 
    return $link;  
  • Yes, it was a typo (edited). But it does not work either. I have added a sentence die('HI!'); at the beginning of the filter function and it only executes in the view article/post action (I saw it at bottom so at the end), but not in a page with blocks of posts, for example. where the links must be well formed for all of those posts.
    – MikeBau
    Jul 12 '19 at 15:35
  • @MikeBau My oversight. The more appropriate filter will be post_link or pre_post_link applied by get_permalink() function. add_filter( 'post_link', 'post_permalink_w_section', 10, 2 );
    – nmr
    Jul 12 '19 at 16:04
  • OK thanks!, now it works fine with add_filter (' post_link ',' post_permalink_w_section ', 10, 2); And finally, before closing the question I would like to know if this is the best way to do this. Thank you.
    – MikeBau
    Jul 12 '19 at 16:29
  • @MikeBau It's a good way, from what I know.
    – nmr
    Jul 12 '19 at 16:43
  • Excellent, thanks. In addition I will add a redirect 301 so that there are no duplicate urls, for the case in which someone tries to access the post without including the category in the url.
    – MikeBau
    Jul 12 '19 at 17:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.