Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a main parent page with multiple sub-pages. The main parent page has some custom fields (meta boxes?) with specific values. My intent is to add dynamically these custom fields and their values to all sub-pages. I searched a solution but without success. Is this possible? And how?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

The parent post ID is stored in $post->post_parent. So you can access the parent data by using that ID in get_post_meta().

To get the ancestors use get_post_ancestors( $post->ID ). It returns an array of parent IDs, the last one is the highest. Let’s invent a helper function for the next examples:

if ( ! function_exists( 'get_top_ancestor' ) )
{
    function get_top_ancestor( $post_id )
    {
        $ancestors = get_post_ancestors( $post_id );
        return empty ( $ancestors ) ? $post_id : end( $ancestors );
    }
}

Now we can use that helper in a filter for the_content:

add_filter( 'the_content', 'wpse_78325_parent_meta' );

function wpse_78325_parent_meta( $content )
{
    global $post;
    if ( ! is_page() or empty ( $post->post_parent ) )
        return $content;

    $top_id = get_top_ancestor( $post->ID );

    if ( ! $data = get_post_meta( $top_id, 'demo_data', TRUE ) )
        return $content;

    $extra = sprintf(
        '<p>Meta data <code>demo_data</code> from <a href="%1$s">parent post</a>:</p>
        <pre>%2$s</pre>',
        get_permalink( $top_id ),
        esc_html( $data )
    );

    return $extra . $content;
}

Let’s say you have a parent page Privacy Policy with a custom field demo_data

enter image description here

… and a child page Security

enter image description here

… then the code above would produce this result:

enter image description here


In reply to your comment: You can use the parent post meta wherever you need it. In your case, I would filter wp_nav_menu_args and call wp_nav_menu with a static string for menu.

Sample code, not tested, just a draft. :)

add_filter( 'wp_nav_menu_args', 'wpse_78325_parent_menu_name' );

function wpse_78325_parent_menu_name( $args )
{
    if ( 'primary' !== $args['theme_location'] or ! is_page() )
        return $args;

    global $post;

    $top_id = get_top_ancestor( $post->ID );

    /* prepend this line with a # to switch the logic
    if ( ! $name = get_post_meta( $top_id, 'MenuName', TRUE ) )
        return $args;

    $args['menu'] = $name;
    /*/
    foreach ( $args as $key => $value ) // you can use custom keys here
    {
        if ( $new = get_post_meta( $top_id, 'MenuName', TRUE ) )
        {
            $args[ $key ] = $new;
            unset ( $new );
        }
    }
    /**/    

    return $args;
}
share|improve this answer
    
toscho, your function worked as you described. Now a small clarification. I have the next code in 'header.php' file: <?php wp_nav_menu( array( 'fallback_cb' => 0, 'theme_location' => 'primary', 'menu_class' => 'nav-menu', 'menu' => get_post_meta( $post->ID, 'MenuName', true ) ) ); ?>. 'MenuName' is the name of parent page Custom Field. What I want is that the child pages to copy dynamically this Custom Field with its value from the parent page as an own Custom Field and value. Is this possible? Sorry I was not very clear. –  Iurie Jan 5 '13 at 16:09
    
@toscho You're a magician! Your code again worked! Thank you! And please excuse my boldness, but I have two more questions. 1) How to adapt the last code for multiple Custom Fields, when we know their names? and 2) How to implement more optimally this last code for the multi-level parent pages? –  Iurie Jan 5 '13 at 19:21
    
@toscho I think it's too late and I'm tired :). Forget the second question, the code works also for the multi-level parent pages. Now I am only interested in the first question. –  Iurie Jan 5 '13 at 19:37
    
@Iurie I have added a helper function to get the top parent and an example to use multiple custom fields, showing how to overwrite the menu arguments. You can use a custom array of keys instead of $args to provide the keys to search for. –  toscho Jan 5 '13 at 19:45
    
@toscho I think that the first question is also solved. I will use your first code, because it is not related to navigation menu and it fits for my other purposes. Thank you again! –  Iurie Jan 5 '13 at 19:48
add comment

Your Answer

 
discard

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.