0

How can I make a taxonomy term have a custom permalink? (on a term basis, e.g. /anything/here for a taxonomy called food and a term called burrito)

I don't think there's much more to explain so, my ideas are:

  • pre_get_posts, $_SERVER to detect url saved against an options table.
  • Register hidden post type that matches each term id. Still how to create custom nested permalinks?

1 Answer 1

1

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!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.