I'm trying to tweak my search results template to include the book root title at the bottom of every search results snippet. I have several books with very similar titles and content and i'd like to be able to print title of the book below each search result snippet so users know what they're going to get when they click search result.

I tried simply print $node->book['bid'] but it didn't print any result. I think it's because i have to somehow get the bid of a snippet and i'm getting bid of search results page which has no bid.

Am i right? And how i do that? I'm using Custom Search module to display my search results.

1 Answer 1


The book id bid is nothing more then the node id nid of the parent. Therefor if you have the bid, you still need to extract the title from the parent node.

$search_hit = $variables['result']['node']->nid; // The node id of the search result item
$node = node_load($search_hit); // Load the whole node whose search result we got
$parent_id = $node->book['bid']; // Extract the parent from the found node

$node2 = node_load($parent_id); // Load the parent node.
echo $node2->title; // This is the parent title.
  • It seems like search results template works in a different way than regular node. I put your code into my custom_search-result.tpl.php and nothing happened. But when i put it in node.tpl.php - it works perfectly. The same piece of code. I cannot even get the node id through simple: <?php print $node->nid; ?>
    – mallin
    Mar 13, 2013 at 14:09
  • Maybe this helps? drupal.org/node/535578 Mar 13, 2013 at 14:13
  • So $this_node = $variables['result']['node']; got me nowhere, basically. I tried to do what you suggested, use this and your example and it didn't return anything. However, i am able to extract node id from this. With your piece of code i did something like this: $node_id = $variables['result']['node']->nid; //This gets me the node id of every snippet $node2 = node_load($node_id); echo $node2->title; // This gets me the node title So now i have a title of the snippet and all i have to do is to get the bid. Any ideas on how to get bid from existing nid?
    – mallin
    Mar 13, 2013 at 16:13
  • You are on the right track, but you need to load also the parent node. With the bid of the child node (the one you display the title from). See the updated example. Mar 13, 2013 at 16:29
  • 1
    Yeah, that was it. Worked like a charm. I was missing loading the whole node when i was trying to get it to work. Now i have a lot of possibilities with conditions based on $parent_id. Also $search_hit can be simplified to $result['node']->nid. Thanks a lot for help! I was trying to get the answer on drupal.org issue page, but noone replied in 9 weeks... Here i got answer in minutes.
    – mallin
    Mar 13, 2013 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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