Custom Rewrite Rule for Custom Post Type with URL Param

SOLUTION: I am answering my own question here since no responses were given and I presume this might be helpful to someone and in the mean while I found a solution.. I had to use the second version of ‘add_rewrite_rule’: add_rewrite_rule( ‘^products/directory/?([^/]*)/?’, ‘index.php?post_type=acme_product&c=$matches[1]’, ‘top’ ); I needed to register a query var,, since external query … Read more

Trouble with question mark in rewrite rule

The root problem here is a misunderstanding of how URLs work. What you see in the browser is not a full URL, but something to make it easier to understand For example, take this URL: http://example.com/test/?foo=bar#bananas Here we have instructions to: Via HTTP protocol aka http:// contact the server at example.com aka https://example.com and do … Read more

Insert post ID into the end of a slug preceeded by a dash

The rewrite argument in register_post_type is limited, but you can define a more complex structure with add_permastruct. The added benefits are that this will generate all the rewrite rules for you, and the new rules will overwrite the originals. You just have to be sure to hook after the post type is registered. function wpd_woo_product_permastruct(){ … Read more

Integer based rewrite isn’t recognized for value of 1

Rewrite rules in WordPress should point to index.php, and to substitute a matched value (number from address) to URL parameter (item_id) you must use $matches[1] instead of $1. So your rule should look like this: add_rewrite_rule(‘^add-item/([0-9]+)/?’, ‘index.php?item_id=$matches[1]’, ‘top’); You usually need to complete the rule with information about what is displayed. For display: single post … Read more

How to rewrite product permalinks in Woocommerce to use category slugs

Ok, so the second code is: function custom_rewrite_basic() { $prodCatArgs = [‘taxonomy’ => ‘product_cat’]; $wooCats = get_categories($prodCatArgs); $catSlugs = []; foreach($wooCats as $wooCat) { $catSlugs[] = $wooCat->slug; } add_rewrite_rule( ‘^(‘.implode(‘|’, $catSlugs).’)/([^/]*)/?’, ‘index.php?post_type=product&category=$matches[1]&product=$matches[2]’, ‘top’ ); flush_rewrite_rules(); } add_action(‘init’, ‘custom_rewrite_basic’);

Best way to trigger rewrite rules

init is the recommended hook and no, the rewrite rules will not be overwritten each time the hook fires or that the page is loaded, unless if you call flush_rewrite_rules() or WP_Rewrite::flush_rules() in your callback. add_action( ‘init’, ‘add_my_rewrite_rules’ ); function add_my_rewrite_rules() { add_rewrite_rule( … ); flush_rewrite_rules(); // don’t do this } So please don’t do … Read more

URL renaming issue

When I go to www.riyachting.com, I get redirected to riyachting.com. Some browsers remove the fragment (part after the #) of the URL in this redirect. Chrome keeps the original fragment and the lightbox works. Safari removes it and the lightbox doesn’t work. So either use the final URL in your link, or use something else … Read more