Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

http://www.mysite.com/area/alabama/birmingham/?action=places

We'd like it to be:

http://www.mysite.com/area/alabama/birmingham/places/

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');

share|improve this question

2 Answers 2

function add_places_rewrite_tags() {
    add_rewrite_tag('%action%','([^&]+)');
}
add_action( 'init', 'add_places_rewrite_tags' );

First add the rewrite tag action (action=xxxx)

function add_places_rewrite_rules() {
    add_rewrite_rule('^area/([^/]*)/places/?','index.php?post_type=area&name=$matches[1]&action=places','top');
    add_rewrite_rule('^area/([^/]*)/([^/]*)/places/?','index.php?post_type=area&name=$matches[2]&action=places','top');
}
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).

share|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

share|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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.