you can add this parameter in URL with this code to put after registering the custom post type :
add_rewrite_tag('%location%', "([^&]+)");
$slug = get_post_type_object("services")->rewrite["slug"];
$query_var = get_post_type_object("services")->query_var;
add_rewrite_rule(
"$slug/([^&]+)/([^&]+)$"
, "index.php?$query_var=\$matches[1]&location=\$matches[2]"
, "top"
);
after adding this in your plugin, you need to flush rewrite rules once :
https://codex.wordpress.org/Function_Reference/flush_rewrite_rules
then you can read the location with get_query_var("location"). the magic of wordpress is that this code works with URL /services/gardening?location=leeds and /services/gardening too.
Related Posts:
- Custom rewrite rules for archive page and single post
- WordPress Rewrite Rules for Custom Post Type and Taxonomy
- How to pass URL parameters for advanced taxonomy queries with multiple terms for one custom taxonomy
- Add category to custom post URL
- Creating a custom public user page
- How to filter custom post type archive by meta value
- Taxonomy archives based on Custom Post Type
- Slug for custom post type archive
- Change page /2 to /transcript with a Rewrite
- Multiple custom post types using the same taxonomy = URL frustrations
- Permalink rewrite with custom post type and custom taxonomy
- Child pages on hierarchical Custom Post Types 404s
- How would I leverage custom Taxonomies in this scenario?
- WordPress returns 404 on custom rewrite rule conflict in parameters
- How to create download links based on Custom Fields with Rewrite API
- How to rewrite custom post type URL for multiple depths instead of one specific depth
- Multiple Permalinks for custom post type post
- WordPress Rewrite rule for nested post types
- Category base to url in custom post type/taxonomy
- Is slug “type” reserved and can’t be used in taxonomy rewrite?
- Custom Post Type get_post_type_archive_link( $post_type ) returns false
- Permalinks when filtering multiple custom post types by single taxonomy
- custom taxonomy archive by year
- How to Combine Two Custom Post Types into Single Permalink Structure
- Redirecting when changing custom post type slugs?
- How can you handle rewrite rules for custom post types with option to filter on custom taxonomy terms?
- Conditional regex in add_rewrite_rule() for specific query filters & pagination
- How to remove slugs from multiple custom post types
- Displaying Posts of a Custom Type
- Can’t change first part of URL rewrite for custom post type
- CPT Archive with core Category
- Custom taxonomy in URL showing 404
- Relative URLs for a particular custom post type?
- Custom Taxonomy URL are redirecting to page with the same name
- Rewrite slug of custom post type to include post-id
- How can we take into account post types when constructing permalinks?
- Taxonomy custom post type URL
- How to change custom post type permalink structure
- Front custom edit post page for each post
- Categories of Custom Post throws gives 404
- How to build custom route by adding taxonomies to URL ? ex: www.demo.com/communities/palo-alto/
- How Can I Change the Custom Post Type Slug to the Taxonomy Slug in the URL?
- Use same slug base for Custom Post Type posts, and multiple taxonomy terms
- Rewrite Question Mark in Post Template to Make it Pretty URL
- How can achieve this permalink perfectly? domain.com/%category%/%post-name%
- Custom Permalink to remove category word , keeping posts permalink with date?
- WP Rewrite Rule Issue while using Custom post type & taxonomy
- WordPress custom post type split into pages
- WordPress uses url different from defined in rewrite arg of custom post type
- Including taxonomy term before post type breaks top level pages
- Correct procedure for advanced permalinks
- Custom Post Types hierarchy and rewrite
- How to get list of taxonomy slugs ordered parents>childs?
- Rewrite rule for Custom Post Type Page as a parent
- Rewrite Endpoints and CPTs – How to use in a plugin
- add_rewrite_rule and passing variables for custom post types
- Custom URLs in post types make it impossible to view archive
- Custom Permalinks for Custom Post Types
- Share root slug for Child Page and Custom Post Type (prioritizing child pages over posts)
- Route subpage to Custom Post Type
- Custom Post Type Category URL
- Programmatically add custom field to post_name in a custom post type
- Custom Post Type Advanced Slug
- Rewrite permalink for multiple post types (CPT) with taxonomy term
- How to constrain the results to a specific post_type on tag archive page?
- Pagination for custom post types – url rewriting
- Modifying rewrite rules to use /category//
- Custom Post Type Date Based Archive URL rewrite [duplicate]
- Custom rewrite rule takes place AFTER the generic category rule
- returning the currect URL for nested posts
- Adding /blog in front of single posts (only)
- multiple URLS for the same page
- 404 error rewrite permalinks
- Custom rewrite rule based on other custom post type title
- Define multiple prefixes for custom post type
- Custom Post Type parent/child relationship rewrite rules for permalinks
- Add %post_id% to slug of custom post type and prevent the “unique slug” thing that WP does?
- Custom Post Type canonical link / pagination redirecting to root
- Rewrite url post_tag for custom post type
- How to build overlapping custom post_type and taxonomy rewrites without conflicts?
- Remove custom post type slug but keep related category taxonomy permalink
- page not found for example.com/custom-post-name
- Force WordPress to only match URL in category
- Custom post type with parent page? Possible?
- current_menu_parent for custom post type and custom url
- Why won’t this rewrite rule work?
- Cannot modify a registered custom post type rewrite slug
- Custom post type and custom taxonomy archive inaccessible
- Generating custom URL before post is published
- Custom post type permalink structure
- How can i change url structure of cpt like this?
- Custom Post & Taxonomy rewrite break regular post & page
- Rewrite Rule for custom post type link with or without taxonomy terms
- Rewrite nested urls for custom post type
- Custom post type Premalinks main page and details page
- Rewrite rule taxonomy url with different values in one function
- Custom URLs between different post types, using “Pods”
- CPT with URL rewrite showing as 404
- How to make a Custom Post Type entry to have different parent pages
- Rewrite taxonomy permalink appended to CPT archive url