0

I have created a custom post type, called 'centres'. This CPT is hierarchical, ie, I can create child pages under each custom post.

Centre1

  • Location1
  • Contact1

Centre 2

  • Location2
  • Contact2

I've defined a custom field called phone_number. This field is populated at the 'parent' level. Ie, Centre1, Centre2.

I want to display the phone number on each child page. I don't want to have to add the phone number every time to each page, so would like to get or inherit the phone_number field from its parent.

Simply speaking, I'd like to just add the shortcode [acf field="phone_number"] in any given child page (eg, contact page) and have it pull the values from it's parent.

I've searched everywhere, but can't find a solution. I'm a novice wordpress user (not a php developer). Is there code I can add into my functions.php file that can get the values of a particular field (or all fields) from the parent?

Much appreciated.

4
  • You'd get better help on ACF support channels. Discussions on the third party plugins are off-topic here.
    – Abhik
    May 12 at 12:58
  • Let's assume then I don't use ACF to create the custom field then.
    – RedWebMan
    May 12 at 13:36
  • You'd create a plugin which adds a shortcode which acquires the parent post ID from the current post object acquired by calling get_post() without arguments, then pass it to retrieve the relevant meta via get_post_meta(). If you want to give that a run and share your work, we can probably help you sort it out - otherwise this might not be appropriate here.
    – bosco
    May 12 at 17:51
  • Thanks for the feedback. I'd seen loads of ACF related questions in this forum for example wordpress.stackexchange.com/questions/111351/… and given the ubiquity of this plugin didn't realise it was off topic. Thanks to TheDeadMedic for providing the answer below.
    – RedWebMan
    May 13 at 0:46

1 Answer 1

1

You can use get_post_field( 'post_parent' ) to get the parent ID of the current post.

Whilst ACF is technically off-topic, FWIW you can pass the ID of a post as the second parameter to get_field.

Combined together we can fashion a shortcode centre_field that will return any field value for the post's parent. I've skipped the need for an attribute name as I think the shortcode looks a little cleaner...

e.g. [centre_field phone_number]

And the code behind it:

function wpse_405660_centre_field( $atts ) {
    // Just grab the first value of the attribute array
    $field_name = current( $atts );

    $parent_id = get_post_field( 'post_parent' );

    return get_field( $field_name, $parent_id );
}

add_shortcode( 'centre_field', 'wpse_405660_centre_field' );
1
  • Thanks! This works. But only for basic text fields. Wonder if you can update your answer to give very basic instructions for future readers? I've edited your answer as comments are not long enough to add my explanation. Apologies if it misrepresents. Edit my edit if I'm saying something incorrectly.
    – RedWebMan
    May 13 at 0:35

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.