Query parameters as slash | WordPress.org

IMO rewrites can be super finicky. If you can, would it be more worthwhile to just convert “Travel” to a taxonomy? Otherwise, I do believe your query var needs to match your query parameter. In the given example you say:


But your code builds for services_centers_type. Whatever you’re using for filtering also needs to be changed from $_GET['type'] to $type = $wp_query->get_query_var( 'services_centers_type' ). Also keep in mind the Reserved Terms you won’t be able to use as query vars.

Finally, I would not do this:

// flush_rules() when adding new rules
function flushRules(){
   global $wp_rewrite;

The above will regenerate your rewrite rules way more than necessary. The rewrite rules are persistent so you only need to save them once. The init hook runs on every frontend and admin request. A better solution may be to simply add in your code and navigate to Settings > Permalinks. This will regenerate and persist all the rewrite rules new and old. It’s the same thing you would want to do with creating a new post type.

Hi @howdy_mcgee

Thanks for the response. So what I changed now is:

1. I have removed the code for flush_rules();

2. "type=tavel", sorry it’s a typo mistake. I am using http://www.example.com/services?services_centers_type=travel

3. On the http://www.example.com/services page, I am already using $services_centers_type = $wp_query->get_query_var( 'services_centers_type' );

But still I am facing the same issue. When I go to http://www.example.com/services/travel, it is still redirecting me to http://www.example.com/services.

Still not able to resolve.

  • This reply was modified 2 days, 22 hours ago by nitinsinghal.

Is Services a post type or is it an actual page you’ve created?

Yes, I have created a page with the with the name services & that is using the template template-services.php.


I’ve run your code on my localhost and it seems to work as expected. If you save permalinks ( by clicking the blue Save button under Settings > Permalinks ) this will regenerate permalinks. If on localhost you can spit out the permalinks to see that your rules appear at the top:

// hook add_rewrite_rules function into rewrite_rules_array
function add_rewrite_rules($aRules) {
	$aNewRules = array('services/([^/]+)/?$' => 'index.php?pagename=services&services_centers_type=$matches[1]');
	$aRules = $aNewRules + $aRules;
	printf( '<pre>%1$s</pre>', print_r( $aRules, 1 ) );
	return $aRules;
 add_filter('rewrite_rules_array', 'add_rewrite_rules');

Then in something like template_include you can spit out the WP_Query object to see if the query var has been populated:

function wpf13639442_template_include( $template ) {

	global $wp_query;
	printf( '<pre>%1$s</pre>', print_r( $wp_query, 1 ) );
	return $template;
add_filter( 'template_include', 'wpf13639442_template_include' );

The die() will end up killing your page to spit out the variables at that point in load. Feel free to error_log those instead. I do see when I pasted your code that the query var does get populated but only after I end up saving the permalinks.

Hi @howdy_mcgee

I have tried that on my localhost on a fresh installation & to my surprise it worked. Then I did some debugging & found out that, I have installed a plugin & it is creating a issue. Plugin is:

https://wordpress.org/plugins/permalink-manager/ .

For my project this plugin is very important. Can you please check & do some help for me on this?

Hi @howdy_mcgee

As I mentioned earlier that I was facing this issue because of that plugin. So I have got the quick solution from the plugin author. I am attaching the solution provided me him, so that if anyone else faces the same problem, he can use that.


Thanks very much for your time as well.


  • This reply was modified 2 days, 2 hours ago by nitinsinghal.

Source link