How can I get the posts from a custom taxonomy using the URL to determine which taxonomy term id is being shown?

I tried to get posts via a category using get_category_by_path which returned false when passing the URL because the custom taxonomy isn't the same. Is there an equivalent function which will do this for taxonomies? I've google and scanned the Wordpress codex and found nothing.

Many Thanks

  • 3
    Might I ask why you would want to do this than getting the current taxonomy ID directly? Something like these answers could be helpful
    – Howdy_McGee
    May 21, 2014 at 21:03
  • @Howdy_McGee Might I ask how get a term Id having its url without visiting that url? :)
    – gmazzap
    May 21, 2014 at 22:44
  • @G.M. You may, but there could be a more efficient way to go about the problem. Without knowing the taxonomy it may be impossible using the vanilla WordPress functions though may be possible with some kind of database query.
    – Howdy_McGee
    May 22, 2014 at 16:04

1 Answer 1


So I'm taking some liberties with this, I'm assuming a couple things:

  1. You know the taxonomy the term belongs to
  2. You have the term archive link

So if I have the term archive link:

$url = 'http://www.example.com/taxonomy/term/';

I know that the last part of that url is going to be the terms slug. I can use the slug with the taxonomy and get the ID but first I need to split the parts:

$parts = explode('/', rtrim($url, '/')); // rtrim() to remove trailing slash

This splits it up in a nice array that looks like this:

Array ( 
    [0] => http: 
    [1] => 
    [2] => www.example.com
    [3] => taxonomy 
    [4] => term 

Note that the term slug is the last item in this array, which is important so we can access it later without really knowing the index.

Next we can use the nifty function called get_term_by() - View Codex, which will allow us to pull the entire term based on the terms slug and the taxonomy the term is in. which looks like this:

$term = get_term_by('slug', $parts[count($parts)-1], 'tax_articles');

And there you go, that will pull the term based off the slug we found in the URL.

  • 1
    +1 This is what I mean when I commented OP. WordPress has API to get an url from an object, but have no API to reverse the thing. This is the reason why I think that the OP is not nonsensical. Regarding your answer, the assumption that the taxonomy url end with term slug is a bit hardcoded even if reasonable, at same point one can change the rewrite argument for the taxonomy to end with something different. Moreover your code easily fail when the url contain pagination variable... WP rewrite stuff is meah
    – gmazzap
    May 22, 2014 at 16:13
  • I agree, it is a bit hard-coded - for all I know OP is using the default WordPress rewrites instead which would break my code entirely. Without seeing the actual URL and the parts there's just a lot of unknowns and scenarios you would have to compensate for.
    – Howdy_McGee
    May 22, 2014 at 16:26
  • 1
    A viable way is to go along rewrite rules, just like core does for url_to_post_id but that's a piece of work...
    – gmazzap
    May 22, 2014 at 16:31

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.