Disabling post_type in URL

I think you might be able to accomplish this via filter. Looking at the core wp class you’ll find the $public_query_vars and $private_query_varsvariables defined by a hardcoded array. These could probably be overriden/changed via the global $wp object but if you dig a bit deeper there’s a filter that should let you remove items from … Read more

Multiple Permalinks for Same Post

The request parsing is handled in WP::parse_request(). After that, there’s a action hook parse_request which gives you the instance of the wp object. We assume that http://www.example.com/my-custom-post-type/this-is-a-cool-article is your permalink and http://www.example.com/mon-type-de-poste-personnalise/cest-un-article-sympa ends up in a 404. So the first thing to check in your callback should be, if the wp object is in a … Read more

Rewrite rules and query for virtual page

You can create a posts archive by just setting post_type=post in your rewrite rule: function custom_archive_rule() { add_rewrite_rule( ‘custom-archive/?’, ‘index.php?post_type=post’, ‘top’ ); } add_action( ‘init’, ‘custom_archive_rule’ ); WordPress will identify this as is_home, so you’ll have to target it in pre_get_posts by the existence of your extra query variables. function custom_arcive_tax( $query ) { if … Read more

WordPress rewrite rules for state and city

Wasn’t as hard as I thought. Here’s the working example, in case anyone finds it helpful: function em_query_vars($vars) { array_push($vars, ‘state’); array_push($vars, ‘city’); return $vars; } add_filter(‘query_vars’,’em_query_vars’); function em_rewrite() { add_rewrite_rule( ‘^state/([^/]*)/([^/]*)/?’, ‘index.php?state=$matches[1]&city=$matches[2]’, ‘top’ ); add_rewrite_rule( ‘^state/([^/]*)/?’, ‘index.php?state=$matches[1]’, ‘top’ ); add_rewrite_tag(‘%state%’,'([^&]+)’); add_rewrite_tag(‘%city%’,'([^&]+)’); } add_action(‘init’, ’em_rewrite’); function em_templates($template) { global $wp_query; if (isset($wp_query->query_vars[‘state’]) && isset($wp_query->query_vars[‘city’])) { … Read more

Change permalink for a single post entry

Try adding the following to your functions.php: function wpse221475_custom_rewrite_rules() { add_rewrite_rule( ‘^(special-project)?’, ‘index.php?post_type=projects&name=$matches[1]’, ‘top’ ); } add_action(‘init’, ‘wpse221475_custom_rewrite_rules’); Make sure to flush your rewrite rules after adding this rule. Visit: Dashboard -> Settings -> Permalinks Or if you wish, you can use flush_rewrite_rules() programmatically. To redirect requests for http://example.com/projects/special-project to http://example.com/special-project you can parse the … Read more

Change Query String to pretty permalink

Try this rewrite rule in your functions.php function string_url_rewrite() { global $wp_rewrite; add_rewrite_tag(‘%pet-page%’, ‘([^&]+)’); //In the rewrite rule you need to enter page slug and page id add_rewrite_rule(‘^Enter_Page_slug/([0-9]+)/?’, ‘index.php?page_id=Enter_Page_ID&pet-page=$matches[1]’, ‘top’); $wp_rewrite->flush_rules(true); } add_action(‘init’, ‘string_url_rewrite’, 10, 0); Now you also need to change the format of URL in anchor tag. For example, your previous URL http://www.example.com/my-account/view-pet/?pet-page=2 … Read more

Date archive permalink modification

You can change the date links by directly modifying the date_structure: function wpd_change_date_structure(){ global $wp_rewrite; $wp_rewrite->date_structure=”news/%year%/%monthnum%/%day%”; } add_action( ‘init’, ‘wpd_change_date_structure’ ); Don’t forget to flush rewrite rules after the change. WordPress will use this to generate the rewrite rules for incoming requests as well as URL output when using API functions like get_year_link, so there’s … Read more

WordPress Rewrite API calls not creating new rules

Correct solution provided by Milo in comment above. Pretty permalinks were set to plain in the admin interface and this evidently causes the WP_Rewrite to silently do nothing. I’m posting the answer here so I can mark something as correct/complete so this shows as answered. Am I doing it wrong?