0

On one of my page templates I need to include the contents of several other pages. For example, if my page template is called Fruit, I need to pull in page content from pages named Apple, Pear, Orange, Lime, etc. This gives my user the ability to edit Apple and Lime individually from admin area.
I'm trying to use get_page_by_title() to pull in these sub pages, but that function only seems to want to work once per page. The first time I call

get_page_by_title('Apple')

works perfectly. When I later call

get_page_by_title('Lime')

it displays Apple again. Is this function limited to once per page, or am I doing something else wrong?

Edit: It seems that no matter what title I pass to get_page_by_title() it will always get the content of the current page. So if I'm on example.com/fruit and call

get_page_by_title('Apples')

with in the page, it will always display the content from Fruit. Not Apples.

1

get_page_by_title returns an id not content. here is an example of what I use to get content from a certain page id.

call the function

$page_id = get_page_by_title('title');
$excerpt =  get_excerpt_by_id($page_id);

in functions.php

function get_excerpt_by_id($post_id) {
   global $wpdb;
   $query = "SELECT post_content FROM $wpdb->posts WHERE ID = $post_id LIMIT 1";
   $result = $wpdb->get_results($query, ARRAY_A);
   return $result[0]['post_content'];
}
  • get_page_by_title() returns post' object or one of numeric and associative arrays, containing post information, not post id. – Max Yudin Jul 18 '12 at 11:38
0

For anyone who finds this, the "correct" answer makes an unnecessary SQL query.

Consider doing:

$page = get_page_by_title('title', OBJECT);
echo $page->post_excerpt;
// Wanna get other values? Check them:
// var_dump($page);
// If you need meta values:
//$val = get_post_meta($page->ID, 'key_of_what_u_want', true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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