How to get custom post type post id from slug?

Is it possible to grab the custom post type post id just only from slug?

As much i know we can get from id by using title. But there can be same title in a custom post type so as slugs are unique is it possible???

3

You can use get_page_by_path() – don’t let the name fool you, third argument is the post type:

if ( $post = get_page_by_path( 'the_slug', OBJECT, 'post_type' ) )
    $id = $post->ID;
else
    $id = 0;

Leave a Comment