Custom post types and custom variables — add_rewrite_tag() question

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 … Read more

How to check if a rewrite rule exists

If I understand correctly then you can hook into the rewrite api/process and flush or manipulate the rules that way? Read: Plugin Hooks on this page http://codex.wordpress.org/Function_Reference/WP_Rewrite Maybe something like: // flush_rules() if our rules are not yet included function my_flush_rules() { $rules = get_option( ‘rewrite_rules’ ); if ( ! isset( $rules[‘(project)/(\d*)$’] ) ) { … Read more

Why does everybody hook add_rewrite_rule on init

You are right saying that without flushing rules, add_rewrite_rule() does not work, and the reason is that WordPress fetches rules from database, and add_rewrite_rule() does not add the rule to database. However, is not possible to use admin_init and the reason is that admin_init is too late. WordPress sometimes calls flush_rewrite_rules() on admin screens before … Read more

When should add_rewrite_tag() be used?

The fundamental difference is: The add_rewrite_rule() adds a particular rule which is interpreted The add_rewrite_tag() adds a placeholder to use in url structures. This placeholder is then used to generate multiple rules. For instance – suppose you’re a travel agent advertising hotels in various countries. You may want a hotel’s url to be like www.example.com/hotels/UK/Balmoral … Read more