How to get post id from permalink (pretty url)?
-
1@Yuliy: That's probably the best answer here; I suggest you make it an actual answer and not just a comment. :) – bcat Nov 02 '10 at 05:15
-
@Yuliy I didn't find, could you help? – Jeaf Gilbert Nov 02 '10 at 05:24
-
1@Jeaffrey Gilbert - Can you explain more what you are trying to actually accomplish? There are several "right" answers, but each is different and each would depend on what your situation is and what you are trying to accomplish. BTW, you might want to post this question on SO's sister site WordPress Answers: http://wordpress.stackexchange.com. – MikeSchinkel Nov 02 '10 at 09:00
-
@MikeSchinkel Im trying to get post ID from custom permalink (%category%/%postname%) by ajax call. +1 for the site. – Jeaf Gilbert Nov 02 '10 at 13:05
-
@Jeaffrey Gilbert - By AJAX call, do you mean in Javascript or in PHP? I still don't follow your context. Can you post some of your code showing where you need to get the post ID? – MikeSchinkel Nov 03 '10 at 03:17
7 Answers
You should be fine with url_to_postid()
[see documentation] which is located in rewrite.php. I used it in a plugin of mine last year, works like a charm.
-
6Well. It is documented now, at least. http://codex.wordpress.org/Function_Reference/url_to_postid – vmassuchetto Apr 11 '12 at 11:27
-
6You should also note the documentation mentions: "Note that this does not return the post id for custom post types. " – Mazatec Nov 11 '12 at 12:47
-
8As of WordPress 3.7.0 also custom post types are supported. See the above link for details. – Simo A. Dec 28 '13 at 21:10
I've got a dedicated (& documented) function for that:
get_page_by_path( $page_path, $output, $post_type );
Retrieves a page given its path.
Where $page_path
is
[...] the equivalent of the 'pagename' query, as in: 'index.php?pagename=parent-page/sub-page'.
See Function Reference/get page by path
Example:
// Assume 'my_permalink' is a post.
// But all types are supported: post, page, attachment, custom post type, etc.
// See http://codex.wordpress.org/Post_Types
get_page_by_path('my_permalink', OBJECT, 'post');

- 1,224
- 1
- 19
- 27
-
1You just need define the post type (see `$post_type` argument: http://codex.wordpress.org/Post_Types) – mems Mar 28 '13 at 00:19
-
You should edit that in. Your answer is the best one now that they have added support for custom_post_types and posts or at least documented it... This is the solution I am using myself. Thanks. – Jake Mar 29 '13 at 17:09
-
1
2022 update
url_to_postid( string $url )
For reference: http://codex.wordpress.org/Function_Reference/url_to_postid

- 2,238
- 2
- 30
- 37
-
1please add more then a link under the word "this" to describe what is behind the link. – jnhghy - Alexandru Jantea Jul 11 '15 at 06:21
-
-
@AdilSoomro sorry about that, answered this a long time ago, link is still alive in way back machine, will update my answer with more details. https://web.archive.org/web/20200729211606/http://betterwp.net/wordpress-tips/url_to_postid-for-custom-post-types/ – deweydb May 17 '22 at 19:14
url_to_postid()
as of 3.7.0
: This function now supports custom post types (see Trac tickets #19744
, #25659
).

- 3,556
- 2
- 24
- 40

- 21
- 2
please use
$postid = url_to_postid( $url );
to retrive the ID of an attachment.
It is required that the url provided be in the format of example.com/?attachment_id=N
and will not work with the full URL to get the id from the full URL.

- 11,330
- 12
- 48
- 63
I have a multisite WP so once I go through the blogs for some reasons in some blogs url_to_postid()
works, in other blogs on the post of the same type it doesn't while get_page_by_path()
works like charm. So I made it this way, which may be not perfect though:
$parsed_url = wp_parse_url( $url ); // Parse URL
$slug = substr($parsed_url['path'], 1 ); // Trim slash in the beginning
$post_id = url_to_postid( $slug ); // Attempt to get post ID
if ( ! $post_id ) { // If it didn't work try to get it manually from DB
$post_query_result =
$wpdb->get_row("SELECT ID FROM {$wpdb->prefix}posts WHERE post_name = '{$slug}'");
$analog_id = (int) $post_query_result->ID;
}

- 306
- 4
- 13
you can try this one also:
$post = get_page_by_path('cat',OBJECT,'animal');
cat is the one you are looking for= the permalink; animal is the custom post type,

- 21
- 2