get wordpress post by post title

You can use get_page_by_title() to fetch post by title.Like this:

$page = get_page_by_title('About', OBJECT, 'post');
echo $page->ID

Details are here.

OR custom query like this :

$posttitle = 'About';
$postid = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = '" . $posttitle . "'" );
echo $postid;

try this

$post_title = 'Contact';
$post_id = get_page_by_title($post_title, OBJECT, 'your_post_type_name');
echo $post_id->ID; // your id

Add Function Like this

function get_page_by_post_name($post_name, $output = OBJECT, $post_type = 'post' ){
    global $wpdb;
    $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", $post_name, $post_type ) );

    if ( $page ) return get_post( $page, $output );

    return null;
}

 add_action('init','get_page_by_post_name');

And run like that:

 $page = get_page_by_post_name('hello-world', OBJECT, 'post');
 echo $page->ID; 

Tags:

Php

Wordpress