Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Quick function to convert a permalink into a term object when inside a custom taxonomy
function get_term_object_from_uri($taxonomy) {
global $wp;
//get requested uri
$wp_request = $wp->request;
//split into array
$uri_params = explode('/', $wp_request);
//drop the product id as unnecessary
$param_total = (count($uri_params)-1);
//get the new deepest level category
$param_top_level = $param_total-1;
$param_slug = $uri_params[$param_top_level];
$param_object = get_term_by( 'slug', $param_slug, $taxonomy );
return $param_object;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment