47

How to get post id from permalink (pretty url)?

Jeaf Gilbert
  • 11,495
  • 19
  • 78
  • 105
  • 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 Answers7

59

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.

Argalatyr
  • 4,639
  • 3
  • 36
  • 62
kovshenin
  • 31,813
  • 4
  • 35
  • 46
  • 6
    Well. It is documented now, at least. http://codex.wordpress.org/Function_Reference/url_to_postid – vmassuchetto Apr 11 '12 at 11:27
  • 6
    You 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
  • 8
    As 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
11

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');
mems
  • 1,224
  • 1
  • 19
  • 27
  • 1
    You 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
    Awesome. hopefully it will help others too. – Jake Mar 30 '13 at 21:40
9

2022 update

url_to_postid( string $url )

For reference: http://codex.wordpress.org/Function_Reference/url_to_postid

deweydb
  • 2,238
  • 2
  • 30
  • 37
2

url_to_postid() as of 3.7.0: This function now supports custom post types (see Trac tickets #19744, #25659).

Hariprasad
  • 3,556
  • 2
  • 24
  • 40
1

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.

Sulthan Allaudeen
  • 11,330
  • 12
  • 48
  • 63
1

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;
}
khandaniel
  • 306
  • 4
  • 13
0

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,