Get WordPress post id from post header

I have a problem with a custom WordPress theme that I am developing. This is a bit confusing, but in essence, I need to get it to get the message "Post Title". In pseudocode, ideally, it would be something like:

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

The title mentions a static link that is not being pulled from WordPress, it is already present on the page.

Thanks in advance.

+46
php wordpress
Oct 08 '09 at 9:33
source share
9 answers

Just a quick note for anyone who comes across this:
get_page_by_title () can now handle any type of message.
The $post_type was added in WP 3.0.

+89
Aug 01 2018-11-11T00:
source share

Found a solution if someone else is struggling with this. Just posted a question out of desperation after 4 hours of 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

+19
Oct 08 '09 at 10:01
source share

As Michal Mau mentioned:

Using

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

This is ( $page_title, $output, $post_type ) to easily get a message instead of a page.

+8
Dec 27 '13 at 15:52
source share

Let it help you more by creating a function so you don't have 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); 
+2
Sep 15 '14 at 7:56
source share

you can use the following code according to [link] [http://codex.wordpress.org/Function_Reference/get_page_by_title] 1 )

 <?php $page = get_page_by_title( 'About' ); wp_list_pages( 'exclude=' . $page->ID ); ?> 
+1
Nov 06 '12 at 5:13
source share

Another way to get the message and page id is to use a plugin.

there is a plugin that it just does, just add a column to all pages, all posts, tables of all categories and an ID column heading ... and at the bottom right you will see all the page / post listed in this column.

I think this should be very helpful ..

I use this plugin very often and it is very light.

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

0
Jun 20 '12 at 19:21
source share

No need to use any SQL queries or plugins, use standard Wordpress functions for this

 $page = get_page_by_title( 'Home' ); $page_id = $page->ID; 
0
Oct 12 '12 at 8:24
source share

easy to get message id from message header using wp request:

 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; 
0
Jul 6 '13 at 12:41
source share

1) post_title and post_name differ from each other. post_name, maybe this is a bullet. post_title is the name of the post.

2)

 $titlee = "yourtitle"; echo $id = $wpdb->get_var("SELECT ID FROM $GLOBALS['wpdb']->posts WHERE post_name = $titlee"); 
0
Oct 11 '13 at 0:34
source share



All Articles