I have this form:

<form action="<?php the permalink(); ?>" method="get" >
<input type="hidden" name="taxo" value="question" />
<select name = "cata">
  <option value='unique_name-a'>xxx</option>
  <option value='foo'>yyy</option>
  <option value='bar'>zzz</option>
<select name ="catb">
  <option value='unique_name-d'>xxx</option>
  <option value='unique_name-e'>yyy</option>
  <option value='unique_name-f'>zzz</option>
<!-- and more select -->

And this query in a template page:

$query = new WP_Query( array(
  'post_type' =>  'page',
  'tax_query' => array(                   
    'relation' => 'AND',                     
        'taxonomy' => 'question',   // from $_GET['taxo']            
        'field' => 'slug',                   
        'terms' => array('unique_name-a','unique_name-e','more'), // from my submit form   
        'include_children' => false,          
        'operator' => 'AND'                    
) );

I would like to play with URL rewriting. I have something like:


I would like the rewritten URL for the above query to be:


EDIT: Why this function is not working?

  function custom_rewrite() {

      // refresh/flush permalinks in the dashboard if this is changed in any way
      add_action( 'init', 'custom_rewrite' ); 

You could try using the add_rewrite_endpoint function, which would actually let you avoid any htaccess modification (unless you need $_GET).


function add_custom_rewrite_ep() {
    add_rewrite_endpoint('cata', EP_PAGES);
    add_rewrite_endpoint('catb', EP_PAGES);
    add_rewrite_endpoint('catc', EP_PAGES);
add_action( 'init', 'add_custom_rewrite_ep' );

Make sure you flush your rewrite rules.

So then, if your URL is /questions/cata/foo/catb/bar/catc/more/, you can access the values with get_query_var like so:

$x = get_query_var('cata','default'); // equals 'foo'
$y = get_query_var('catb','default'); // equals 'bar'
$z = get_query_var('catc','default'); // equals 'more'

It is difficult though to give you an exact solution without knowing how your url's are generated, if you're using the $_GET global, and how you're using the values in your code.

  • I use a form <form action="<?php the_permalink(); ?>" method="get">…</form> in a custom template page. I add your code, but don't know how to deal with it (I flush my rewrite rules). – benoît Sep 29 '16 at 8:55
  • Please post all of your code as an edit to your question. There's no way to tell you how to fix your issue with only tiny pieces of the puzzle. :) – Spartacus Sep 29 '16 at 15:37
  • I try add_rewrite_endpoint questions/cata/foo/catb/bar/catc/more/ is not 404 but ugly url works too /?taxo=question&cata=foo&catb=bar&catc=more/?taxo=question&cata=foo&catb=bar&catc=more and stay ugly. I make a test with this example : codex.wordpress.org/Rewrite_API/… (Template Name: Nutritional Information) and I have the same issue with the ugly url (stay ugly). – benoît Sep 29 '16 at 16:56
  • I found some documentations to complete the official codex : hardcorewp.com/wp-content/uploads/2014/03/… (most complete) code.tutsplus.com/articles/the-rewrite-api-the-basics--wp-25474 (clear) pmg.com/blog/… – benoît Oct 2 '16 at 9:26
  • So did you get it to work? – Spartacus Oct 3 '16 at 20:08

Here's my approach to it using the following RewriteRule in the .htaccess instead of using the add_rewrite_rule() function:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} ^taxo=([^&]+)&([^=]+)=([^&]+)&([^=]+)=([^&]+)&([^=]+)=(.*)$
RewriteRule ^/?$ /%1/%2/%3/%4/%5/%6/%7? [L,R=301]

This has been tested and verified using the htaccess tester.

The difference between Spartacus' answer and mine is that condition is met if the query string has ?taxo= in it before it starts the rewrite rule. Also, my rewrite isn't bound to only cata, catb, and catc which you are using for example purposes. This rule dynamically covers all of your other slugs, in case your taxonomy values were to change. For example:


Will become:

  • It's work, but if someone flush the rewrite rules, I need to rewrite the htaccess. The next step is to add this rule with WordPress function. And I have another problem, I did not anticipate my action to get query results in a custom page<form action="<?php the_permalink(); ?>" method="get">…</form>, I don't know how to deal with my permalink http://example.com/mypagename/?taxo=… I try RewriteRule ^mypagename/?$ /%1/%2/%3/%4/%5/%6/%7? [L,R=301]. – benoît Sep 29 '16 at 9:09
  • 2
    If you add your custom rewrite rules outside #BEGIN WordPress #END WordPress, your rewrite rules will not be erase with a flush rewrite. WordPress only touch what is inside these tags. – Benoti Sep 29 '16 at 21:06
  • @benoît Update my answer based on Benoti's comment. – Ethan O'Sullivan Oct 1 '16 at 14:39
  • @EthanJinksO'Sullivan it's work but WordPress can't find the page (404). This is the part 1 of the answer. The Spartacus's answer is another part 1 of the answer, but I think I can find the part 2 (rewriting my form and my functions.php). – benoît Oct 2 '16 at 9:17
  • @benoît I've updated my answer to explain the difference between Spartacus' answer and mine. May you update your question to elaborate on what you're trying to achieve in "part 2"? – Ethan O'Sullivan Oct 2 '16 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.