WordPress get post ID by post title
February 15th, 2011 at 11:34am Jason 3,009 views
When I was making MR J DICT, I want to get post id by title,but there is no template tags (get_post_by_title)directly to realize it. If you know a post id ,you can get the post title or content or slug very easily by using get_post($post_id).Still,if you want to Get page ID by title,you can use get_page_by_title.
BUT if you want to get the post id by title?When you have get or know the post title,how to get the post id? Here the codes below for you to test in sidebar.php of your template.
Firstly Get post ID by post title through querying the wordpress database,then you can use get_post($post_id) to get whatever you want to echo.
<?php $posttitle = 'Hello world!'; $postid = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = '" . $posttitle . "'" ); echo $postid; ?>
<?php $getpost= get_post($postid); $postcontent= $getpost->post_content; echo $postcontent; ?>
In order to make you understand how to query the database, here are two more samples just the same way as step 1.
<?php $posttitle = 'Hello world!'; $post_slug_name = $wpdb->get_var( "SELECT post_name FROM $wpdb->posts WHERE post_title = '" . $posttitle . "'" ); echo $post_slug_name; ?>
<?php $posttitle = 'Hello world!'; $post_content = $wpdb->get_var( "SELECT post_content FROM $wpdb->posts WHERE post_title = '" . $posttitle . "'" ); echo $post_content; ?>
Thanks get-wordpress-post-id-from-post-title
Entry Filed under: WordPress Tagged with: wordpress
Hot Posts Hot Comments Hot Tags
- apartment bbpress blog class CMS code CSS div dream english friends girl Google hot Hot Dog html job JS life lines love Madonna menu Michael Jackson movie oral english PHP plugin saying school sexy site student students submenu success tags teacher teaching txt university video website widget wordpress





3 Comments Add your own
1. Gino | February 19th, 2011 at 1:08 pm
Thank’s it help me a lot for a new website i’m working now.
http://www.starsclips.com/
Checking if title already exist to add new post from user.
more to come it’s only alpha now
2. Jocke | May 26th, 2011 at 3:04 am
Hi, just stumbled at your blog. And since I hate doing direct sql in wordpress I figured out there has to be another way.
So here it is:
ID;
?>
It’s not superpretty but I prefer using the wp_query instead of sql.
3. Prasad | May 30th, 2011 at 3:55 pm
Nice Article.
Here you will get lot of stuff regarding to WordPress.
Visit http://www.wordpressstop.com
Keep Smiling.
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">
Trackback this post | Subscribe to the comments via RSS Feed