I'm stuck on this, and have no idea what I'm doing. I have a custom post type called areas, that is hierarchical. We want to pass a variable into the page, and have it be pretty. Currently, it is:


We'd like it to be:


I've been mucking around with add_rewrite_tag() now for several hours, and just can't get this to work.

function create_custom_rewrite_rules() {
    global $wp_rewrite;

    // Define custom rewrite tokens
    $rewrite_tag = '%places%';

    // Add the rewrite tokens
    $wp_rewrite->add_rewrite_tag( $rewrite_tag, 'area/(.+?)/places/', 'action=places' );

    // Define the custom permalink structure
    $rewrite_keywords_structure = $wp_rewrite->root . "area/%pagename%/$rewrite_tag/";

    // Generate the rewrite rules
    $new_rule = $wp_rewrite->generate_rewrite_rules( $rewrite_keywords_structure );

    $wp_rewrite->rules = $new_rule + $wp_rewrite->rules;

    return $wp_rewrite->rules;

function add_custom_page_variables( $public_query_vars ) {
    $public_query_vars[] = 'action';
    return $public_query_vars;

add_action( 'generate_rewrite_rules', 'create_custom_rewrite_rules' );
add_filter( 'query_vars', 'add_custom_page_variables');
function add_places_rewrite_tags() {
add_action( 'init', 'add_places_rewrite_tags' );

First add the rewrite tag action (action=xxxx)

function add_places_rewrite_rules() {
add_action( 'init', 'add_places_rewrite_rules' );

Then add a custom rewrite rule. (post_type=area may be different if you made a custom slug for the posttype, use the posttype name).

Remember to flush your rewrite rules to make this work (go to your permalink settings).

| improve this answer | |

Here's another question very similar to yours with a working answer... See if it helps.

Custom page with variables in url. Nice url with add_rewrite_rule

| improve this answer | |
  • Nope, that's not any real help. Possibly because I don't understand the regex they are doing. – FunnyKitty Oct 25 '11 at 20:19

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.