get-post-by-title-in-wordpress

How to get post by title in WordPress

  • July 4, 2022

Here is the snippet to get post by title in wordpress

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;
}

If you want to get the custom post by title in wordpress just change post_type to your custom post type.

Related Post

Fix-Images-and-Broken-Links-by-Updating-Paths-in-WordPress

Fix Images and Broken Links by Updating Paths in WordPress

Today we will learn how to fix broken images link issue in WordPress, after migrating site from local server to live server or (one domain to another). This is a very common issue. The simple […]

Insert post programmatically in WordPress

Insert post programmatically in WordPress

Today we are gonna learn how to insert post in database programmatically. The function we are going to use is wp_insert_post() For creating a post first you need to create a post array, below is […]

Hide product category or product on shop page

Hide product category or product on shop page

Hello Guys today we gonna learn how can we hide specific product category, specific product on shop page. We are going to do this with the help of code snippets. You can directly add these […]