Our Wordpress website has a custom post type named "life".

For each "life" post , A specific custom field value "special" (true | false) is set.

I want to rewrite the URL only if the custom field value "special" is "true" so that I can easily filter only those posts with special=true in Google Analytics.

  • I don't want to change the post type itself.

  • I also considered adding the taxonomy name to the URL, but this affetcs other posts.

  • I don't want to change the URL of posts where the "special" value is NULL or false.

  • I am thinking about changing the permalink structure with a hook or something , or forcing parameters to be attached to the URL.

  • URL examples I want to realize (life-article is slug name and "special" field is true)
    • https://example.com/life/special/life-article/
    • https://example.com/life/life-article/?special=1

  • If the post's "special" value is false or NULL , URL should be
    • https://example.com/life/life-article/

This is a permalink setting of "life" in Custom Post Type Permalinks. enter image description here

  • Permalink setting screen
    • https://example.com/wp-admin/options-permalink.php

  • I tried the following hooks, but it didn't work because I guess it wasn't compatible with the Custom Post Type Permalinks plugin.
    • add_rewrite_rule
    • post_type_link

  • The "life" posts have %postname% in the permalink. Is it possible to rewrite this to a URL like /special/%postname%/?

  • Since the Custom Post Type UI plugin is used to add the post type, I would appreciate it if you could tell me how to do it without using register_post_type() function. Or is it possible to overwrite the value set in register_post_type()?

1 Answer 1


I would stick to make the url https://example.com/life/life-article/?special=1

I would create a php function that:

  • retrives all posts and stores them in a variable;
  • make a loop to loop all the posts and check if they are "life" posts;
  • inside the same loop, if they are "life" posts, change their slug to ...whatever/?special=1;

and then run the function one time somehow.

this should do the trick. Sorry I dont have posts websites, only products websites, so I do not have a proper code, but this is my idea, and here goes a example code;

function change_life_slugs(){
    $posts = some_function_that_retrieves_all_posts();
    foreach($posts as $post){
       if($post_type->type == "life"){
          wp_update_post([  // this is actually the function to update posts
              "post_name" => "new-slug/?special=1",
              "ID" => $post->id

this is what I would do, then just put

<?php change_life_slugs(); ?>

on for example front-page.php and go the website main page one time. after that, once its done, just remove the code from the website. and all "life" would have special equals true on the url. Sorry for not having the real code, but this the idea design , the functions needed I am sure you find it easily. cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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