I'm curious if it is possible, in a plugin/custom code, to take an URL that has been generated by the currently running WordPress site & parse it to retrieve the $blog_id and $post_id?

ie. taking a string like "myblog.mysite.com/2012/03/16/whatever/" and determining that this is post #6 ("whatever") on blog #2 ("myblog").

I realize that I can probably strip the URL and parse the domain myself, but I hope that there might be recommended WordPress methods exposed to make this more robust across URL formats.

3 Answers 3


As Weston mentioned, if you're within WordPress and you know what blog the URL came from, then ask WordPress for the blog ID.

But if you're really, really sure there's no better method in this case than parsing a URL, then here you go:

$url = 'http://myblog.mysite.com/2012/03/16/whatever/';

$domain = parse_url( $url, PHP_URL_HOST );

// This could be dynamic but good enough for now
$subdomain = str_replace( '.mysite.com', '', $domain );

// Never use $blog_id for anything, you'll break tons of things
$parsed_blog_id = get_id_from_blogname( $subdomain );

// url_to_postid() only works for the current blog
switch_to_blog( $parsed_blog_id );

$post_id = url_to_postid( $url );


Reference material:



  • Thanks Viper! That's exactly what I was looking for (I need to reverse a pasted URL, not parse the current one) Although str_replace() works to get blogname, and explode('.', $domain)[0] would work too, I am wondering if there is a way to handle the case where SUNRISE is enabled (multi-domain mapping.) The wp_blogs table is actually stored with full URL so it'd be even simpler than get_id_from_blogname! Thanks BTW for your work on WordPress! Mar 18, 2012 at 5:28
  • I forgot to mention that you need to call switch_to_blog() before calling url_to_postid() as the function doesn't parse the domain only the path. I've updated my code. And no problem! :) Mar 19, 2012 at 21:11

Plain and simple, to get the post id you just use:

echo get_the_ID(); 

That will echo out the post id.


echo get_current_blog_id(); 

Will echo out the blog id.



http://codex.wordpress.org/Function_Reference/get_current_blog_id respectively

  • 1
    Thanks for the answer Weston, but I need to parse an URL in a string, not get info from the current URL. Appreciate the help anyway. Mar 18, 2012 at 5:32

The best solution I've found for this is the Monkeyman Rewrite Analyzer plugin. It was written by a member of the WPSE community in response to a few similar requests some time ago.

You can give it any URL for your site, and it will tell you explicitly how that request will be mapped/interpreted by WordPress.

For example: Rewrite analyzer screenshot

  • Thanks, I'll see if I can figure out how it works internally (since I need to get the results in code, rather than via a UI) It might be a more thorough answer since it handles all forms of URLs and not just post slugs... Mar 20, 2012 at 21:14

Your Answer

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

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