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. :)– bcatNov 2, 2010 at 5:15
-
@Yuliy I didn't find, could you help?– Jeaf GilbertNov 2, 2010 at 5: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: wordpress.stackexchange.com.– MikeSchinkelNov 2, 2010 at 9:00
-
@MikeSchinkel Im trying to get post ID from custom permalink (%category%/%postname%) by ajax call. +1 for the site.– Jeaf GilbertNov 2, 2010 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?– MikeSchinkelNov 3, 2010 at 3: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. codex.wordpress.org/Function_Reference/url_to_postid Apr 11, 2012 at 11:27
-
4You should also note the documentation mentions: "Note that this does not return the post id for custom post types. "– MazatecNov 11, 2012 at 12:47
-
6As of WordPress 3.7.0 also custom post types are supported. See the above link for details.– Simo A.Dec 28, 2013 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');
-
1You just need define the post type (see
$post_type
argument: codex.wordpress.org/Post_Types)– memsMar 28, 2013 at 0: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.– JakeMar 29, 2013 at 17:09
-
1
2022 update
url_to_postid( string $url )
For reference: http://codex.wordpress.org/Function_Reference/url_to_postid
-
1please add more then a link under the word "this" to describe what is behind the link. Jul 11, 2015 at 6: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. web.archive.org/web/20200729211606/http://betterwp.net/…– deweydbMay 17 at 19:14
url_to_postid()
as of 3.7.0
: This function now supports custom post types (see Trac tickets #19744
, #25659
).
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;
}
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,
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.