add_rewrite_rule and custom variable

Hi @westondeboer:

Since you are saying your post type is 'portfolio' here’s what works on my WordPress v3.0.4 test site with a 'portfolio' custom post type defined. It replaces all the code you have:

add_action('init','yoursite_init');
function yoursite_init() {
  global
  $wp,$wp_rewrite;
  $wp->add_query_var('view');
  $wp_rewrite->add_rule('portfolio/([^/]+)/all',
    'index.php?view=all&post_type=portfolio&name=$matches[1]', 'top');

  // Once you get working, remove this next line
  $wp_rewrite->flush_rules(false);  
}

You also need to flush your rewrite rules which you can do by saving your Permalinks in the admin console. Let me know if this solves your issue and if not please give me clarification.

UPDATE

You also need to capture the query parameter differently than using `$_GET[‘view’]’; either of these will work:

// Option 1
$view = {$GLOBALS['wp']->query_vars['view'];

// Option 2
global $wp;
$view = $wp->query_vars['view'];

Leave a Comment