WordPress | Function get_page_by_title() retrieves a post given its title. If more than one post uses the same title, the post with the smallest ID will be returned.
Because this function uses the MySQL ‘=’ comparison the $page_title will usually be matched as case insensitive with default collating.
1 | get_page_by_title( string $page_title, string $output = OBJECT, string|array $post_type = 'page' ) |
File: wp-includes/post.php
Examples 1: Find Page ID to use with exclude in wp_list_pages
1 2 3 4 | <?php $page = get_page_by_title( 'About' ); wp_list_pages( 'exclude=' . $page->ID ); ?> |
Examples 2: How To Find WordPress Page ID By Title Then Replace the_content()
1 2 3 4 5 6 7 8 9 | function my_content($content) { $page = get_page_by_title( 'Sample Page' ); if ( is_page($page->ID) ) $content = "Hello World!"; return $content; } add_filter('the_content', 'my_content'); |
Examples 2: How to Find WordPress Custom Post Type by Title
1 2 | $mypost = get_page_by_title('World Peace Now', OBJECT, 'link'); print_r($mypost); |
Refference: https://codex.wordpress.org/Function_Reference/get_page_by_title
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.