0

I'm attempting to implement a rewrite rule so I can introduce a variable into my CPT's single page template. Basically I'm trying to include 'all' between my CPT's slug name of work and the category in the URL:

http://localhost/work/all/corporate/cognitive-puzzles/

The original URL should of course work as well:

http://localhost/work/corporate/cognitive-puzzles/

Here's what I have for my rewrite:

function wire_alternate_pdp_url() {
    global $wp;
    $wp->add_query_var('mode');
    add_rewrite_rule('work/all/([^/]*)/([^/]*)/?', 'index.php?pagename=$matches[2]&mode=all', 'top');
}

This almost works but for two issues. First being, the URL in my browser switches back to the form with the missing all/ path. Second and more importantly, I get nothing for a mode within my single-project.php:

<?php 
    $mode = get_query_var('mode');
    echo $mode;
?>

This prints out nothing.

Any ideas on what I might be missing here? I feel I'm close but something isn't quite right.

Thanks in advance!

edit: forgot to mention, I'm running 3.6.1

  • It looks like your rule is not matching and some other rule is (so WP redirects you to correct URL). Are you sure that your rule is correct and it's matching? Also... When are you calling this wire_alternate_pdp_url function (on which filter/action hook)? – Krzysiek Dróżdż Nov 16 '13 at 21:48
  • I am calling wire_alternate_pdp_url on the init hook. I'm pretty certain the rule is matching. As an experiment, I changed the rule to this: add_rewrite_rule('work/all/([^/]*)/([^/]*)/?', 'index.php?pagename=foo&mode=all', 'top'); and this resulted in URLs redirecting to a foo page I had made. – wittyhandle Nov 16 '13 at 22:27
0

wow, after more banging of my head against the wall, it took this:

add_rewrite_rule('work/all/([^/]*)/([^/]*)/?', 'index.php?post_type=project&name=$matches[2]&mode=all', 'top');

Two things actually, I had to include a post_type=project part as well as change pagename to name. If I remove either one, I get my previous results.

I am not sure why this set of values works when other examples didn't have this, but glad it works :)

| improve this answer | |

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.