Leverage WP_Rewrite to pre-validate links / detect invalid links
You could call get_post_type with the id to check its type before outputting the permalink.
You could call get_post_type with the id to check its type before outputting the permalink.
I recognize that rule. It’s the unusual rule I wrote to deal with the permalink uncertainty issue. 🙂 First, you’ll only get that weird rule if you have your custom permalinks set to start with a non-numeric value. Like, if you have your custom permalink string as just %postname% instead of something like %year%/%postname%. Next, … Read more
Your regular expression is requiring the last slash be present which it will not be by default. So the path tides/mylocation/ is shortened to tides/mylocation and then tested. Instead, wrap the last part in an optional group using the ? and update the match number. add_rewrite_rule(‘tides/([^/]+)(/([^/]+))?’, ‘index.php?page_id=4348&location=$matches[1]&month=$matches[3]’, ‘top’);
That’s going to the non wp rules because you’re not mapping the URL to the index.php page format. If you want it to be a WP rule, then you need to start the destination with index.php and include the query string to define what you want the query to contain. So if you wanted it … Read more
doesn’t it work get_query_var(‘u’) to grab the variable ? Also you need to use – “index.php?pagename=search-result&u=”. ‘$matches[1]’ , single quote surrounding $matches[1].
ok this i didn’t know i had to “register” variable to use function my_add_rewrite_rules() { global $wp,$wp_rewrite; $wp->add_query_var(‘book_id’); add_rewrite_rule(‘^books/([^/]*)/reviews/([0-9]+)/?$’, ‘index.php?page_id=227&bookid_id=$matches[2]’, ‘top’); // Once you get working, remove this next line $wp_rewrite->flush_rules(false); } add_action(‘init’, ‘my_add_rewrite_rules’); and in page-reviews.php i am using $book_id = $GLOBALS[‘wp’]->query_vars[‘book_id’]; One last question: how do i fix my breadcrumbs as the books/…/reviews … Read more
The second one works because post_type=market triggers the market post type archive. Pages have no archive, so post_type=page in the first example doesn’t point to a valid destination. Rewrite rules have to ultimately result in a successful main query. For a specific page, you could use page_id or pagename.
To answer my own question, I didn’t need to remove the ‘redirect_canonical’ filter, and I only needed to change the 2nd parameter for add_rewrite_rule. The resulting function looks as follows: function add_custom_rewrites() { add_rewrite_tag(‘%category%’, ‘(.+)’); add_rewrite_rule(‘^gallery/([^/]*)/?$’, ‘index.php?pagename=gallery&category=$matches[1]’, ‘top’); flush_rewrite_rules(); } add_action(‘init’, ‘add_custom_rewrites’);
You are already using pages which does not work the same as posts. You have to remember, pages don’t have taxonomies and they are excluded from the main query by default on all archive pages and the homepage, so there can never be any links to any kind of archive. Archive pages, whether date, category … Read more
All rewrite rules should point to index.php. This is a reference to the main index.php file through which all requests are routed, not a template file.