• I am using the default post type ‘post’ of the WP as the ‘articles’ for one of my projects. I have a case where I’d like to use a custom taxonomy called ‘article_types’, defined explicitly for the post type ‘post’, to be used in permalinks like this. /%articles_types%/%postname%.html

    I am also looking for a way to enforce it programmatically so that it is not accidentally changed using the permalink settings page.

    Things I’ve tried so far.

    1. Just as a proof of concept, I first decided to set the permalink structure using the UI. First thing I notice, I don’t see the ‘%article_types%’ placeholder to select from (I think it’s called perma_struct in WP Lingo). Still I tried manually typing it in the custom permalink field and hit save. The result is that I do see the article URLs changed to this format but the %article_types% is not getting replaced with the assigned “article types” to the article. Like so,/%article_types%/name-of-the-post.html instead of the /news/name-of-the-post.html.

    2. I tried this same thing with code, using the $wp_rewrite’s set_permalink_structure() function. Since it’s the same thing as #1, I didn’t see anything different. The online material shows that I’ll have to use the filter pre_post_link and replace the %article_types% with whatever the value that is assigned to the post.

    My doubts with above approaches is that I suspected the placeholder will be replaced automatically by the WP but it isn’t. Am I doing something wrong? Or it is perfectly fine but it’s just that article_types is defined on a particular post type only, I would have to handle the case manually.
    And that brings another doubt in the picture, when I set the permalink_structure using the function, am I inadvertently asking WP to add that placeholder for all the post types? Are the pages and other default post types going to get affected by it? Or the permalink settings page is only specifically for the ‘post’ post type? I am asking this because the documentation of the set_permalink_structure method states that it Sets the main permalink structure for the site.

    3. This approach is different from the other two where I tried hacking around with the post type arguments while the registration of the default post type. So basically, I did this,

    add_filter( ‘register_post_type_args’, function ( $args, $post_type ) {
    global $wp_rewrite;
    if ( $post_type === ‘post’ && ! empty( $wp_rewrite ) ) {
    $args[‘rewrite’] = array(
    ‘slug’ => ‘/%article_types%/%postname%.html’,
    ‘with_front’ => false,
    );
    }

    return $args;
    }, 10, 2 );

    But still no luck. The permalinks of the ‘post’ remain as is. When I say as is, I mean to say that they follow what has been set in the set_permalink_structure OR the permalink settings page.

    How should I approach this problem? I would try to avoid creating a custom post type named articles at this point. Any other ideas are highly appreciated.

Viewing 1 replies (of 1 total)
  • Moderator Imagebcworkz

    (@bcworkz)

    There’s probably a number of ways to accomplish what you want. What I would do is add a custom rewrite rule. However, your desired /%articles_types%/%postname%/ permastruct is far from ideal because it’ll conflict with child page URLs. I strongly recommend that you include some static element in your permastruct, for example /type/%articles_types%/%postname%/. This way the only possible conflict is if you had child pages whose ancestor slug is “type”.

    The regexp of your rewrite rule would match the “type” element and capture the two subsequent elements. The captures can then be used to rewrite to something like index.php?article_type=first-capture&name=second-capture. This rewritten URL will not be visible externally, it’s only used within WP itself.

    You will need to flush current rewrite rules for an added rule to take effect. If you’re adding a rule to an existing, active plugin or theme, the easiest way to flush rules is to visit the permalink settings screen. No need to change or save anything, loading the screen is enough. It only needs to be done once, the rules are persistent.
    https://developer.wordpress.org/reference/functions/add_rewrite_rule/

Viewing 1 replies (of 1 total)

The topic ‘How to use custom taxonomy in the permalinks of default post type ‘post’?’ is closed to new replies.