0

taxonomy type property_city rewrite slug is = property-city

taxonomy type property_type rewrite slug is = property-type

all this taxonomy is registered for post_type 'property'

The current URL structure is this: post_type: https://web.com/property/property-name

taxonomy type property_city https://web.com/property-city/name-of-city/

taxonomy type property_type https://web.com/property-type/name-of-type/

So the main question can I make change taxonomy rewrites of each one to achieve the URLs like this:

The idea is if add to the property city some property type in URL and just show the properties according to of this filter (certain city and if is set certain type) with pagination

I hope I am clear enough.

All that a found here is some fixed URL like this /city/city-name/type-name/ its simple but can it be possible when URL is dynamic? And what is the best way to do this?

3
  • Best practice of nested taxonomies is to avoid it. You should really understand why you doing this (and that is basically answer on question how you doing this) before going that. Apr 27, 2018 at 16:44
  • Why? Because "The idea is if add to the property city some property type in url and just show the properties according of this filter"
    – anseme
    Apr 27, 2018 at 16:50
  • Because of implementation complexity. I did it in past, I would not recommend do it. You basically need to know what sub terms have all posts that fit parent terms selection, and only then generate links for terms. Sound simple? But its actually complex task. Apr 28, 2018 at 9:17

1 Answer 1

0

The rewrite rules to resolve these requests are fairly simple:

function wpd_property_rules(){
    add_rewrite_rule(
        'property-city/([^/]+)/([^/]+)/page/?([0-9]{1,})/?$',
        'index.php?property_city=$matches[1]&property_type=$matches[2]&paged=$matches[3]',
        'top'
    );
    add_rewrite_rule(
        'property-city/([^/]+)/([^/]+)/?$',
        'index.php?property_city=$matches[1]&property_type=$matches[2]',
        'top'
    );
}
add_action( 'init', 'wpd_property_rules' );

There are a couple of problems though. You'll need to generate these links somehow, but there's no relationship between two taxonomies except by looking at all posts to see what terms they share. The other issue is that since there is no hierarchical relationship between taxonomies, template tag output for these are somewhat random. For example, a page with both city and type will "identify" itself as a city archive, when you could argue it would more appropriately be a type archive, since cities are further divided by type.

1
  • is posible to disable "city archive" or shold i? Or may be is better solution to make relations between both taxonomies? What you think?
    – anseme
    Apr 30, 2018 at 16:26

Your Answer

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

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