• 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

    (@bcworkz)

    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'].

    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.