WordPress Rewrite Rules

First you have to filter query vars and add your query var to the array: add_filter( ‘query_vars’, ‘wpa56345_query_vars’ ); function wpa56345_query_vars( $query_vars ){ $query_vars[] = ‘cat_id’; return $query_vars; } Then your rule which captures any digits after your pagename coupons and passes that as cat_id: add_action( ‘init’, ‘wpa56345_rewrites’ ); function wpa56345_rewrites(){ add_rewrite_rule( ‘coupons/(\d+)/?$’, ‘index.php?pagename=coupons&cat_id=$matches[1]’, ‘top’ … Read more

How to Add Rewrite Ruled Argument Into Permalink Properly

I’m affraid this “nasty” solution would be the most effective than trying to write some cool function that will translate your permalinks into pretty permalinks: $permalink = rtrim( get_permalink( $id ), “https://wordpress.stackexchange.com/” ) . ‘/foo’ . ‘/2’; The rtrim part (removing trailing slash) is just for to be sure there are not going to be … Read more

Rewrite for page with a possible unknown parent page

You can use the pattern (([^/]*)/)? to match a full <component>/ construction – this will create a multi-dimensional element in the $matches array, with the first level containing the full expression and the internal matches containing an item with <component> without the “https://wordpress.stackexchange.com/” character. However, you need to be very careful in the way you … Read more

Where to hook to bypass instantiating WP_Query?

While it’s possible to get rid of WP_Query in WP load process it’s pretty horribly inconvenient to accomplish. 🙂 Let’s recap the process as related to rewrite: Web server takes pretty permalink and rewrites it to WP’s index.php. WP takes “pretty” part and applies its rewrite rules to it, turning it into query variables. Query … Read more