I’ve done something similar to what you are after here, but I was simply filtering by a meta key.
add_action('init', 'add_custom_meta_url');
function add_custom_meta_url() {
global $wp,$wp_rewrite;
$wp->add_query_var('food');
$wp_rewrite->add_rule('food/([^/]+)','index.php?food=$matches[1]','top');
$wp_rewrite->flush_rules(false); // This should really be done in a plugin activation
}
And then apply this to the index query through query vars (you’d have to target the taxonomy values instead of the custom meta and adjust the compare to =):
add_action('parse_query', 'apply_custom_meta_to_query');
function apply_custom_meta_to_query(&$query) {
if (isset($query->query['food'])) {
$query->query_vars['meta_key'] = 'food';
$query->query_vars['meta_value'] = $query->query['food'];
$query->query_vars['meta_compare'] = 'LIKE';
}
}
There could be more efficient ways to do this, but keep looking at wp_rewrite
, as that’s the WordPress way to handle pretty URLs rather than try to hack into the flow of things.
Good luck!