Title: Adding Rewrite Rule
Last modified: August 22, 2016

---

# Adding Rewrite Rule

 *  [citstudio](https://wordpress.org/support/users/citstudio/)
 * (@citstudio)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/adding-rewrite-rule-1/)
 * hi there,
 * I Create
 * function add_query_vars($aVars) {
    $aVars[] = “type”; return $aVars; }
 * add_filter(‘query_vars’, ‘add_query_vars’);
 * function add_rewrite_rules($aRules) {
    $aNewRules = array(‘career/application-
   form/job-opportunities/([^/]+)/?$’ => ‘index.php?pagename=application-form&type
   =$matches[1]’); $aRules = $aNewRules + $aRules; return $aRules; }
 * add_filter(‘rewrite_rules_array’, ‘add_rewrite_rules’);
 * But when I go to career/application-form/job-opportunities/
    the page redirect
   to career/application-form/
 * on the page, I create :
    if(isset($wp_query->query_vars[‘type’])) { $sMsdsCat
   = urldecode($wp_query->query_vars[‘type’]);
 * to display job-opportunities.
 * But it doesn’t work.
 * Thank’s

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/adding-rewrite-rule-1/#post-5424942)
 * It appears you are not getting the ‘type’ value out of the query_vars. Did you
   declare `global $wp_query;` or use `get queried object()` so that `$wp_query`
   actually contains a WP_Query object?
 * Getting the correct query object on a template page is not 100% reliable, if 
   you are not getting your ‘type’ parameter that way, try using `$_GET['type']`.
 *  [Chris](https://wordpress.org/support/users/zenation/)
 * (@zenation)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/adding-rewrite-rule-1/#post-5425005)
 * Rewrite rules can be a pain in WordPress. In order for new rules to really apply
   you might have to flush the stack.
 * To do that you can either go to the permalilink settings page and simply hit “
   save” (no need to do an actual change) or -and I would recommend doing that anyway-
   run `flush_rewrite_rules();` at the end or your add_rewrite_rules() function,
   before you return the new array.
 * Or/and you could use the “ini” hook instead. This might not be the official way
   but seems to do the trick sometimes.
 *     ```
       function add_custom_rewrite_rules() {
   
         add_rewrite_rule(
           'career/application-form/job-opportunities/([^/]+)/?$',
           'index.php?pagename=application-form&type=$matches[1]',
           'top'
         );
   
         flush_rewrite_rules();
   
       }
       add_action( 'init', 'add_custom_rewrite_rules' );
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Adding Rewrite Rule’ is closed to new replies.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 2 replies
 * 3 participants
 * Last reply from: [Chris](https://wordpress.org/support/users/zenation/)
 * Last activity: [11 years, 6 months ago](https://wordpress.org/support/topic/adding-rewrite-rule-1/#post-5425005)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
