1

Actually I needs to change the structure of my urls for the custom post type ( I also want to customise taxonomy url but only targeting post for this question to avoid confusion)

My custom post type is Property having slug properties and it have a taxonomy 'Property Category' having slug property_category

Requirement

Currently the structure of custom post type is as follow: https://domain.com/properties/postname

For custom post type I want to include categories and sub categories in the url and remove properties slug like https://domain.com/property_category/property_sub_category/**/postname

By using the plugin (Custom Post Type Permalinks) I am able to get this url https://domain.com/properties/property_category/property_sub_category/**/postname

As you see that my above url have properties in it and I have to remove it so everything is working fine till here for me.

After that, I have removed the properties from url and add function to check my property custom post type along with posts and pages so my url becomes https://domain.com/property_category/sub_category/**/postname but it breaks means giving me 404.

This url is working but I didn't need this one https://domain.com/property_postname

I believes that Wordpress ReWrite API can solve this problem but unable to figure out the actual solution for it.

Any help in this regard will be greatly appreciated.

1 Answer 1

0

The problems that I face is already described in detail. As I guess that wordpress rewrite api can solve this problem so I have found that solution. I only have two main category for my posts so I map those to my properties because as I am removing custom_post_type name from url so there should be something to identify and map the url.

/**
 * Rewriting url
 */
function mak_rewrite_add_rewrites()
{

    // Mapping Property
    add_rewrite_rule(
        '^category_one_name(/[\S]*)+(/[\S]*)$',
        'index.php?property=$matches[2]',
        'top'
    );

    add_rewrite_rule(
        '^category_two_name(/[\S]*)+(/[\S]*)$',
        'index.php?property=$matches[2]',
        'top'
    );
}
add_action('init', 'mak_rewrite_add_rewrites');

$matches[2] represents to get value from second parenthesis in regular expression.

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.