I have a database of locations and services, made using WordPress , each town / region / county is a custom taxonomy,
I wrote some custom rules to make slug clean but they (site owners) ask to remove town/region/county words! I tried but I can’t achieve it, can anybody help me?

these are my URL examples

/services/home-care/region/norfolk
/services/home-care/town/norbury

and these are my rules

 add_rewrite_rule('^services/(.*)/town/(.*)?', 'index.php?post_type="ag_location"&ag_primary_inspection_cat=$matches[1]&ag_cities=$matches[2]', 'top');
    add_rewrite_rule('^services/(.*)/region/(.*)?', 'index.php?post_type="ag_location"&ag_primary_inspection_cat=$matches[1]&ag_regions=$matches[2]', 'top');
    add_rewrite_rule('^services/(.*)/county/(.*)?', 'index.php?post_type="ag_location"&ag_primary_inspection_cat=$matches[1]&ag_county=$matches[2]', 'top');

I want to have

/services/home-care/norbury
/services/home-care/norfolk

but don’t think it’s possible!
I have Vue.js codes in those archive pages that fetch data based on town or region or county type.

I guess can map all of them into 1 page, and in that page check type of term.
like

 add_rewrite_rule('^services/(.*)/(.*)?', 'index.php?post_type="ag_location"&ag_primary_inspection_cat=$matches[1]&ag_county=$matches[2]', 'top');

and in page taxonomy-ag_county.php

$term = get_queried_object(); // Get the term 
$slug=$term->taxonomy; 
$placeType="";

switch ($slug){ 
    case 'ag_county': $placeType="county"; break; 
    case 'ag_cities': $placeType="city"; break; 
} 

I have a search form that loads those taxonomies like this:

enter image description here

1 Answer
1

Changed URL formats to be like:

/services/home-care/COUNTY/TOWN

add_rewrite_rule(
    'services/([^/]*)/([^/]*)/([^/]*)/?',
    'index.php?post_type="ag_location"&ag_primary_inspection_cat=$matches[1]&ag_cities=$matches[3]',
    'top' );


add_rewrite_rule(
    'services/([^/]*)/([^/]*)/?',
    'index.php?post_type="ag_location"&ag_primary_inspection_cat=$matches[1]&ag_county=$matches[2]',
    'top' );


add_rewrite_rule(
    'services/([^/]*)/?',
    'index.php?post_type="ag_location"&ag_primary_inspection_cat=$matches[1]',
    'top' );

Based on this solution

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *