Wordpress - show parent post meta for current post
// from here http://wordpress.stackexchange.com/questions/149164
function show_parent_post_meta( $page = NULL, $meta_key ) {
if ( is_numeric( $page ) ) {
$page = get_post( $page );
} elseif( is_null( $page ) ) {
$page = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : NULL;
}
if ( ! $page instanceof WP_Post ) return false;
// if we are here we have a valid post object to check,
// get the ancestors
$ancestors = get_ancestors( $page->ID, $page->post_type );
if ( empty( $ancestors ) ) return false;
// ancestors found, let's check if there are featured images for them
global $wpdb;
$metas = $wpdb->get_results(
"SELECT post_id, meta_value
FROM {$wpdb->postmeta}
WHERE meta_key = '{$meta_key}'
AND post_id IN (" . implode( ',', $ancestors ) . ");"
);
if ( empty( $metas ) ) return false;
// extract only post ids from meta values
$post_ids = array_map( 'intval', wp_list_pluck( $metas, 'post_id' ) );
// compare each ancestor and if return meta value for nearest ancestor
foreach ( $ancestors as $ancestor ) {
if ( ( $i = array_search( $ancestor, $post_ids, TRUE ) ) !== FALSE ) {
return $metas[$i]->meta_value;
}
}
return false;
}