This seems like a fairly simple idea, and forgive me for my ignorance, but in searching and testing various options, none of them work at all. I have tried a number of htaccess ideas and also WordPress's add_rewrite_rule the following:
- https://wordpress.stackexchange.com/questions/56417/url-rewrite-with-add-rewrite-rule-and-attachment-id/
- https://matty.blog/custom-url-rewrites-in-wordpress/
- Custom rewrite not working
I have a WordPress url that looks like this:
https://mywebsite.com/?listing_category=Stay
I want the URL to look like this:
If I can get that working, it should then be straight forward to add additional parameters to the URL and build user friendly permalinks such as:
https://mywebsite.com/?country=Italy&listing_category=Stay => https://mywebsite.com/italy/stay/
Any guidance on this would be most appreciated ;-)
Here is some of the code I have tried:
Option 1
function rewriteurl() {
global
$wp,$wp_rewrite;
$wp->add_query_var('listing_category');
$wp_rewrite->add_rule('^/([^/]*)/?$', 'index.php?listing_category=$matches[1]', 'top');
// Reset permalinks, delete after programming
$wp_rewrite->flush_rules(true);
}
add_action( 'init', 'rewriteurl' );
Option 2
function custom_rewrite_rule() {
add_rewrite_rule('^([^/]+)?','index.php?listing_category=$matches[1]','top');
add_rewrite_tag('%listing_category%', '(.*)');
flush_rewrite_rules();
}
add_action('init', 'custom_rewrite_rule');