I've scoured the Codex, failed to get get_page_by_title() to work and am quite surprised that there doesn't seem to be a standard WP function for this task.

I need to get the ID of any given post/ cpt or page using either the slug of the post/ page title. Ideally I'm looking for the following:

get_post_ID_by_title('My post title', 'customposttype');

What should I be doing?

  • How were you using get_page_by_title and is the custom post type hierarchal, i don't think get_page(called by get_page_by_title) will work for non-hierarchal types.
    – t31os
    Commented Mar 6, 2011 at 11:00
  • Yes, the custom post type in question is non-hierachal.
    – jnthnclrk
    Commented Mar 6, 2011 at 11:15
  • Ignore previous comment regarding being hierarchal, that shouldn't matter, how were you calling get_page_by_title?
    – t31os
    Commented Mar 6, 2011 at 23:27

5 Answers 5


Update: As of WordPress 3.0 you can use the built-in function get_page_by_title with the third parameter $post_type:

$post = get_page_by_title( 'Post Title', OBJECT, 'post_type' );

$post_id = $post ? $post->ID : 0;

Original Answer

you can use this function that jumps by google "get post by title"

* Retrieve a post given its title.
* @uses $wpdb
* @param string $post_title Page title
* @param string $post_type post type ('post','page','any custom type')
* @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A.
* @return mixed
function get_post_by_title($page_title, $post_type ='post' , $output = OBJECT) {
    global $wpdb;
        $post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", $page_title, $post_type));
        if ( $post )
            return get_post($post, $output);
    return null;
  • 1
    So far I've managed to avoid using DB queries, as I prefer to use standard WP functions. Is this really the only way to achieve what I need?
    – jnthnclrk
    Commented Mar 6, 2011 at 11:14
  • The code posted is pretty much a direct copy of the get_page_by_title function, the major difference here is that it'll work for non-hierarchal types, so it's one worth having in your function collection.
    – t31os
    Commented Mar 6, 2011 at 11:25
  • @t31os I never said it was mine i only added the $post_type paean to it and yep its going to my collection.
    – Bainternet
    Commented Mar 6, 2011 at 11:45
  • I wasn't accusing you of anything mate, if anything i'm supporting your answer.. ;) You already got my +1.. :)
    – t31os
    Commented Mar 6, 2011 at 12:13
  • @t31os I know ,its just a bad joke :)
    – Bainternet
    Commented Mar 6, 2011 at 12:29

You are missing a function parameter. Throw a null in for the second optional paramter $output and this function should work for you.

get_page_by_title('My post title', null, 'customposttype');

I just ran into the same issue and adding the null fixed it for me.

  • 1
    That null should really be OBJECT for clarity. But, yes he was essentially throwing the post type in as the output type by leaving out the output type. Commented Jul 29, 2017 at 23:05

I tend to shy away from direct DB queries. Instead, I use the WP_Query object to parse things for me.

This is, basically, a function I use in one of my themes to get a post based on a given slug:

function get_post_id( $slug, $post_type ) {
    $query = new WP_Query(
            'name' => $slug,
            'post_type' => $post_type


    return get_the_ID();

This will create a query using the WP API to fetch a post of a specific type with a given slug, will run the regular loop functions on the result, and return the ID of the post. You could also use it to return the entire post by modifying the function a bit, but that's up to you.

  • Instead of $query->the_post() which alters the loop and will disrupt any get_the_xxx functions after it, rather use: if ($query->have_posts()) { return $query->posts[0]->ID; } else { return 0; }
    – Mikepote
    Commented Dec 14, 2015 at 13:07

This is a few years old now, but it still pops up when searching for this on Google. So, here's a simple way to do it:

$page = get_page_by_title( 'my post title' );


$page = get_post_ID_by_title('my post title', OBJECT, 'customposttype');

Then just use the $page object to grab the ID:


Currently (WP >4.9.2) you'll find your "any" given post/page etc. by title/slug this way:

$page = get_posts(
        //'name'      => 'your-post-slug', // by post slug
        'title'      => 'Your post title', // by post title
        'post_type' => 'page' // post type of your preference

if ($page = $page[0]) // First/lowest ID taken if many objects
    // Then you do whatever is needed...
    // $id = $page->id;
    // $content = $page->post_content;
    // or $content = apply_filters('the_content', $page->post_content);
    // etc.

Your Answer

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

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