The code you are using looks valid, so…
Before going any further, check to see that the post you are testing with actually has the term you are expecting to find (and replace). In other words, verify your input is valid.
Also, you might consider just doing a straight string replace on the URL as, if the search term doesn’t exist, no harm will be done. That said, if the search term does exist in the post name, for example, it will have unintended consequences so, you might also consider making the search more specific (like adding / (slash) to the search term).
Assuming the input is valid, you might also consider making this change in your rewrite rules (in apache or nginx) rather than in PHP. The reason is if you ever move to a new theme, for example, the functionality won’t change (and it might even be faster than processing every request in PHP).
Related Posts:
- How to create a permalink structure with custom taxonomies and custom post types like base-name/parent-tax/child-tax/custom-post-type-name
- Remove taxonomy slug from a custom hierarchical taxonomy permalink
- Shouldn’t this be easy?! Custom post type/custom taxonomy permalink
- Custom Post Type Permalink / Rewrite not working immediately
- Custom Post type & Taxonomy URL structure
- Combining Multiple Taxonomies in one URL
- Why does a custom post type need the ”hierarchical’ args setting?
- Templates for Custom Post Types and Custom Taxonomies
- How to modify URL structures in custom post types and taxonomies or terms
- Custom permalinks
- array_pop() expects array, boolean given for $event_type_term ->slug
- custom post type permalinks with category and tags
- Permalink rewrite with custom post type and custom taxonomy
- Add links to post categories and taxonomies in wysiwyg link modal?
- Custom Permalinks for Custom Post Types and Taxonomies
- Custom Post Type with Nested Taxonomy and Template Files
- Semantic URL Custom Post Type and Taxonomies permalinks
- How to rewrite custom post type URL for multiple depths instead of one specific depth
- Maintaining WP_Query Relation Among CPT, Custom Taxonomy, Permalinks- CPT UI
- Custom post type category, taxonomy and URL rewrite problem
- Custom Post Type: How to display all of same taxonomy?
- Remove “-2” from a Toolset Types URL with the same post name
- Remove Taxonomy Slug when No Taxonomy is Assigned to Custom Post Type?
- Include taxonomy slug in url?
- Custom permalink – append taxonomy name and term name at the end of permalink
- custom post type and custom taxonomy url conflict
- Add hierarchical taxonomy to permalink for custom post type
- Custom post types, taxonomies, and permalinks
- How to specify URLS for custom posts & taxonomies
- Custom post types, taxonomies, and permalinks
- Permalinks not working for custom taxonomy on custom post type
- custom permalinks based on taxonomies for custom post type
- Custom taxonomy in URL showing 404
- Custom Post Type permalink shows wrong page: homepage/index.php
- 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
- create a subcatagory for a custom post type and out put url as posttype/subcategory/item
- custom-taxonomy/post_name instead of the default post_type/post_name
- Show custom post archive when custom post not specified
- Show custom post type relationships by taxonomy
- Permalinks: custom post type -> custom taxonomy -> post
- CPT’s with landing page instead of archive – Url Rewrite?
- Custom post type permalink tag 404
- Hierarchical permalinks for custom post type and taxonomy
- set permalink for CPT
- How to solve this custom post type goes to 404 page?
- How to Set Taxonomy Object Description?
- Template for custom post type when taxonomy is in the URL
- Permalink problems with custom post type and custom taxonomy
- Custom Post Type Advanced Slug
- WordPress custom permalink and archive link for custom post type
- Ideas on how to organize a project [closed]
- Enabling permalinks disables custom page template
- Change permalinks for custom post type based on taxonomy term
- Custom post type category permalinks and archive pages
- Safe to throw 404 error in request filter?
- Set priority to permalinks
- Is there a way to use ‘publicly_queryable’ => false only on specific taxonomy term?
- How to prefix custom post type URL with custom post taxonomy term?
- How do you create pillar content pages?
- Changing CPT permalink
- Custom post type single page returning 404 error when sharing archive url with custom taxonomy
- Changing permalink of default Post with custom post
- Custom Taxonomy Archive generates 404
- Permalinks: custom post type -> custom taxonomy -> custom sub taxonomy -> post
- Customise Permalink Structure For Custom Post Types
- % encoded URL giving 404 error in WordPress
- How to create a permalink structure with custom taxonomies and custom post types
- Custom post type URL structure with site.com/custom_taxonomy_slug/post_name
- Pagination not working (page/2 retunring 404 page)
- Custom post type post_type_link gives “page not found” on any other custom post type
- Custom permalink with custom taxonomies for custom post type
- Template hierarchy changes when custom post-type permalink leverages custom taxonomy
- Post types showing tables…Can I do that?
- Associate custom CPT with another CPT’s taxonomy and rewrite permalinks
- Change the url of a custom post type to include taxonomy
- Post Custom & Taxonomy 404 error
- Multiple archives for cpt, taxonomies, terms
- Same permalink structure for all post types?
- Multiple post types or single post type with category taxonomy?
- How to add base permalink to custom posts?
- Hierarchical taxonomy in permalink structure
- Permalinks when using Custom Post Type with static page for archive
- How to change permalinks to taxonomy and post type’s posts
- taxonomy term names in custom post type permalink structure
- Adding 2 CPT with multiple custom taxonomies to permalinks
- Parent cpt/child custom post type URL permalink relationship
- Custom Post Type Permalinks Problem
- Change Taxonomy Permalinks
- Category / Custom Post Type permalink issue
- Adding rewrite rules to Custom Post-type mixed with Taxonomy
- Remove slug from Custom Category Permalink + dual-category permalinks
- How to have a dynamic slug and rendering the archive-{post_type}.php?
- Add /blog to the “post” post type url
- Different CPT permalink base on taxonomy
- Hide custom post type slug url from search engine [closed]
- Is it possible for post and custom post type to share a category base slug?
- Permalinks for Custom Post Types and Taxonomies
- How does WordPress manage to differentiate between post and page URLs without a distinct base, and how can I replicate this functionality?
- How to correctly edit permalink structures for both default and custom posts/taxonomies?