generate_rewrite_rules didn’t work for me at all and I found posts on forums where others were having the same issue. Adding to the rewrite_rules_array did work. Below is the solution.
add_filter( 'post_link', 'custom_permalink', 10, 3 );
add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
add_filter('init','flushRules');
function custom_permalink( $permalink, $post, $leavename ) {
$category = get_the_category($post->ID);
if ( !empty($category) && $category[0]->cat_name == "Shop" )
{
$permalink = trailingslashit( home_url('shop/' . $post->post_name ) );
}
return $permalink;
}
function flushRules(){
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function wp_insertMyRewriteRules($rules)
{
$newrules = array();
$newrules['^shop/(.*)$'] = 'index.php?name=$matches[1]';
return $newrules + $rules;
}
Related Posts:
- Posts URL structure like site.com/category/the-post-title
- Filtering categories in the permalink structure
- Multiple post categories – single permalink
- Preventing index.php?category_name=something from redirecting
- Add forward slash on categories url (serve one version of a url)
- how to use a different domain/subdomain for authors/catagories on single site?
- Custom post types with their own permalink structure
- Rewrite category wordpress
- Categories’ hierarchy in URL
- Category and tag with same name
- Rewrite category slug
- Changing the category permalink structure
- How to? Redirect /category/sci-fi/ to /category/scifi/?
- Exclude subcategories from the url
- How to display only the parent category in the permalink
- Plural Category Base Slug On Category Archive Page
- Custom Category/Subcategory structure
- One WP install, I need to map a custom domain name to each category
- How to show in search results posts with a particular tag in a particular category?
- Categories not working – 404 Not found error
- How to set up Author archives with sub category URL
- force category base – archive pages work WITH & WITHOUT category base slug in url?
- Add “page-a” slug to category link if user visit category link from “page-a”
- Rewrite a category
- How do I get rid of “category” from my URL structure?
- One Specific Category To Permalink
- How to change category and year archive permalinks/rewrite rules?
- Read GET parameters in APACHE Mod Rewrite rules for WordPress categories in Posts
- Add /category/ to an author archive page
- Permalinks: display just one kind of wp category in the posts url
- Multiple URL’s based on category for one post
- Remove subcategory and category with some rules from permalink
- WordPress URL Rewrite for dynamic and customized URL
- Flat category URLs but retaining hierarchy?
- Custom category URL rewriting
- How do I add a tag slug to a category URL to filter posts?
- How to link a PDF file to a category URL
- Custom url structure – pages under a custom post type
- Category URL Management
- Redirect Problems with Archive Page and GET variables
- Include specific category in URL automatically
- remove url from categories
- Using custom structure prefix showing 404 for sub-categories
- Add custom categories and subcategories and posts as custom pages
- A different permalink structure for different categories
- How to set the same base url for two different taxonomies?
- How to remove parent category from child category url using wordpress rewrite rule?
- How do I rewrite a single category link to point to a custom page?
- Remove “category” from permalink with add_rewrite_rule
- Category and tag urls return 404 error
- WordPress Category url redirects to subdomain
- Hide category name using mod_rewrite
- Hide category name in URL
- Create monthly archive for parent category
- hierarchical taxonomy in permalinks? using same slug
- Modify / Rewrite single template url structure
- What approach should I take for this URL structure?
- Remove “?category=” in the URL wordpress ajax filter work for category
- wordpress category rewrite rule with pagination
- Create a category list page
- How do I use the same post slug for different Categories?
- How to display only 3 main categories, separated by commas, since they are marked in the post?
- How to tax query X number of posts related by tag first, then by category if not enough in tag-related
- Woocommerce product categories order [closed]
- How do I create a way for users to assign categories to a post from the frontend?
- How to hide a specific category posts in my monthly archive?
- Give specific category its own permalink structure
- Automated adding of one tag to all the posts in a category
- How to set up sub-categories for author pages?
- Assign different category colours to different categories in the main menu
- wp_list_categories, Add class to all list items with children
- Using separator with wp_list_categories
- Multi-site or categories?
- View the latest created categories
- category hierarchy level as a body class – parent cat =1, child cat=2, grandchild=3
- Are Category or Tag Archive Pages Possible?
- How to Exclude post from a category
- Hiding a Categories content on just the Homepage ‘Posts’?
- How to change a post category when the ACF datepicker Field is today
- Two separate portfolio pages [closed]
- Child categories doesn’t echo if it doesn’t contain at least a post
- About title on a page
- Displaying year once in category.php [duplicate]
- Restrict CSS on one page
- Check is category parent with ids from the childs – get_term_children
- Showing posts from 2 categories only on category.php
- Exclude categories from postquery
- Category pagination not working
- How can I set a product’s url to reference the primary category in Woocommerce?
- displaying the last product comment in the woocommerce category
- Show Posts via cat+cat in URL that are in both Categories doesn’t work
- Private categories in WordPress
- How to prevent WordPress from redirecting single page to prefixed category archive with same name?
- How to edit woocommerce sub-category page
- Hide specific category tag on single post page
- Custom Archive Template for Multiple Categories
- Question About Messing With Category Pages and Descriptions
- how to get postmeta info based on multiple categories?
- How to exclude category and post_tag taxonomy while displaying custom taxonomy?
- List Terms by category