Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a post by title in Wordpress?

Tags:

wordpress

Wordpress 3.0

I want to have the contents of a specific post into a page by using the title of the post. As far as I can tell, I can't do it directly with get_post().

I can assume what the brute force way might be, but I suspect there's a more elegant way?

like image 277
johnjohn Avatar asked Aug 28 '10 15:08

johnjohn


People also ask

How do I get post title and content in WordPress?

So to get value of title field of seo plugin use this line of code in your function in functions. php: get_post_meta( $post_id, $key, $single );

How do I make a single post title in WordPress?

single_post_title( string $prefix = '', bool $display = true ): string|void.


2 Answers

<!--1.Get post ID by post title if you know the title or the title variable-->
<?php
$posttitle = 'post_title';
$postid = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = '" . $posttitle . "'" );
echo $postid;
?>

<!--2.use get_post($post_id) to get whatever you want to echo-->
<?php
$getpost= get_post($postid);
$postcontent= $getpost->post_content;
echo $postcontent;
?>
like image 123
Luke Avatar answered Oct 19 '22 10:10

Luke


get_page_by_title($id, OBJECT, 'post');

There ye go.

like image 43
AlxVallejo Avatar answered Oct 19 '22 08:10

AlxVallejo