get ancestors post-thumbnail in search results <p>I've a page (companies) with two subpages (new, featured).<br> The subpages query stuff from a custom post-type (companies). In the subpages there's also a search box that filters stuff from the custom post type.<br> Then, there's a banner that reads the post-thumbnail of the page "Companies" and that banner should be visible in the subpages too. I wrote this function and it works just fine for both subpages but not for the search results. It replaces the banner with the post-thumbnail of the first result, instead of the superparent (companies).<br> How can I fix this?.</p> <pre><code>function get_parent_post_thumb( $post ){ if ( $post-&gt;post_parent ) { $parentId = end( $post-&gt;ancestors ); echo get_the_post_thumbnail( $parentId, 'banner' ); } else { the_post_thumbnail( $post-&gt;ID, 'banner' ); } } </code></pre> <p><strong>EDIT:</strong> I got it working with a different approach, but not as dynamic... I just put an empty field with the parent's id in the search form and then I retrieve it in the <code>search.php</code> template.</p> <pre><code>&lt;input type="hidden" name="parentId" value="129" /&gt; </code></pre> <p>And then...</p> <pre><code>&lt;?php $id = $_GET['parentId']; echo get_the_post_thumbnail($id, 'banner'); ?&gt; </code></pre> <p>This is not ideal but it works. Does any one have an idea on how to do it with the function I posted in the original question?</p>