Not getting post by title in WordPress

Viewed 172

I have to try belove code to get post by title but I am not able to get a post

$data = get_posts( array( 'post_title' => 'Gallery','post_status'=>'inherit') );

this gives me a null response and some it gives all post list

3 Answers

Try to retrieve your posts by wpdb class as follows:

global $wpdb;
$myposts = $wpdb -> get_results($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_title LIKE '%s'", '%'. $wpdb->esc_like($title) .'%'));

foreach ($myposts as $mypost) 
{
    $post = get_post($mypost);

    // add your code here for a post processing

}

Try with following.

$args = array('s' => 'Gallery','posts_per_page' => -1);

$your_searchposts = get_posts($args);

OR

$args = array('s' => 'Gallery','posts_per_page' => -1,'post_status'=>'any');

$your_searchposts = get_children($args);

you can get post by bellow code:

$args = array("post_type" => "mytype", "s" => $title);
$query = get_posts( $args );
Related