WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Just wondering how to fetch a page by using 'title contains'. I can retrieve a page by using the following

$page = get_page_by_title('Restaurants and Pubs');

but how could I do 'page title contains "Pubs"'?


share|improve this question
up vote 1 down vote accepted

You should build a query using the Wordpress database class. Place this in your functions.php file

function get_page_by_title_search($string){
    global $wpdb;
    $title = esc_sql($string);
    if(!$title) return;
    $page = $wpdb->get_results("
        SELECT * 
        FROM $wpdb->posts
        WHERE post_title LIKE '%$title%'
        AND post_type = 'page' 
        AND post_status = 'publish'
        LIMIT 1
    return $page;

Use this in your theme:

$page = get_page_by_title_search('Foo Bar');
echo $page->page_title;
echo $page->page_content;
share|improve this answer
Actually I'm just looking to pull a page into my template. For example, this drags in the 'Restaurants and Pubs' page... <?php function callpage($page) { $my_id = $page; $post_id = get_post($my_id, ARRAY_A); $content = $post_id['post_content']; return $content; } ?> <div id='somediv'> <?php – SparrwHawk Sep 18 '11 at 20:52
Then this would be your answer. I updated the answer to show you how to use this. – Brian Fegter Sep 18 '11 at 20:55
Sorry, I only managed to half write my answer, and it won't format my code in the comments (grrr). What I was meant to say is that this works perfectly... jsfiddle.net/NnaqM/1 , but I want to drag in a page by just typing in pub i.e. not type out the whole title, so something like this $page = get_page_by_title(contains 'Pubs'); – SparrwHawk Sep 18 '11 at 21:02
When I say 'typing' I mean all in the backend template, nothing to do with the search box in the interface. – SparrwHawk Sep 18 '11 at 21:05
Just make this into a function (see my latest edit). There's no need to use get_post() because the entire post object is returned from the query. – Brian Fegter Sep 18 '11 at 22:00

This works pretty well for me. Would love to hear feedback on better ways to do it though. Thanks!

// Get all pages
$my_pages = get_pages();

// Get ID's of pages with "string" in title
foreach ($my_pages as $my_page) {

    if ( stristr($my_page->post_title, 'string') ) {

        $include_ids .= $my_page->ID . ',';



// Output only pages with "string" in the title
$args = array(
  'include' => $include_ids,

wp_list_pages( $args );
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.