Get WordPress Post ID from Post title

PhpWordpress

Php Problem Overview


I have an issue with a custom WordPress theme I'm developing. It's a bit convoluted, but essentially, what I need to do is get a Post Id by it's Post Title. In pseudo-code it would ideally be something like:

title = "foo";
post_id = get_post_id_where_title_is(title);

The title mentioned is a static reference not being pulled in from WordPress, it's already present on the page.

Thanks in advance.

Php Solutions


Solution 1 - Php

Just a quick note for anyone who stumbles across this:
get_page_by_title() can now handle any post type.
The $post_type parameter has been added in WP 3.0.

Solution 2 - Php

Found a solution if anyone else struggles with this. Only posted the question out of desperation after 4 hours testing/Googling!

function get_post_by_title($page_title, $output = OBJECT) {
    global $wpdb;
        $post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type='post'", $page_title ));
        if ( $post )
            return get_post($post, $output);
 
    return null;
}

Found at: http://sudarmuthu.com/blog/2009/09/18/retrieving-posts-and-pages-based-on-title-in-wordpress.html

Solution 3 - Php

Like Michal Mau mentioned:

Use

$my_post = get_page_by_title( 'My Title', OBJECT, 'post' );
echo $my_post->post_content;

It's ( $page_title, $output, $post_type ) to easily receive a post instead of a page.

Solution 4 - Php

May this will help you more by creating function so that you need not to repeat the code

function get_page_id_by_title($title)
{
$page = get_page_by_title($title);
return $page->ID;
}

$title = "your title";
get_page_id_by_title($title);

Solution 5 - Php

you can use the following code as per [a link]http://codex.wordpress.org/Function_Reference/get_page_by_title )!

<?php 
$page = get_page_by_title( 'About' );
wp_list_pages( 'exclude=' . $page->ID );
?>

Solution 6 - Php

Another way to get the post and page ID, is to use a plugin..

there is a plugin, that what it simply does, is just add a column to your all pages, all posts, all categories tables, and have a column title of ID...and right below, you will see all the page/post id listed in that column..

I think that should be very useful..

I use this plugin very frequently and it is very lightweight.

http://getyourblogready.com/?p=758

Solution 7 - Php

No need to use any type of SQL querys or plugin, use Wordpress standard functions for this

$page = get_page_by_title( 'Home' );
$page_id = $page->ID;

Solution 8 - Php

it is easy to get the post id from post title using wp query:

global $wpdb;

$rw = $wpdb->get_row( $wpdb->prepare("select * from "your post table name" where post_title='your variable name or your post title'"));

echo $rw->ID;

Solution 9 - Php

  1. differ post_title and post_name from each other. post_name maybe is the slug. post_title is the title of post.

$titlee = "yourtitle";
echo $id = $wpdb->get_var("SELECT ID FROM $GLOBALS['wpdb']->posts WHERE post_name = $titlee");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAaronView Question on Stackoverflow
Solution 1 - PhpMichal MauView Answer on Stackoverflow
Solution 2 - PhpAaronView Answer on Stackoverflow
Solution 3 - PhpBrainBUGView Answer on Stackoverflow
Solution 4 - PhpKundan SInghView Answer on Stackoverflow
Solution 5 - PhpRamkumarView Answer on Stackoverflow
Solution 6 - Phpuser1470261View Answer on Stackoverflow
Solution 7 - PhpErik LarssonView Answer on Stackoverflow
Solution 8 - PhpOmdevView Answer on Stackoverflow
Solution 9 - PhpT.ToduaView Answer on Stackoverflow