Search code examples
phpwordpressadvanced-custom-fieldscustom-post-type

WordPress: Get ACF Post Object selection (Post ID) and use it with $post = get_post();


I'm having a hard time understanding how to use two PHP snippets together.

My situation: The standard WordPress Post Type "Pages" has got its own ACF field "header-selection". This field is a Post Object and the return format is the Post ID. A selection can be made from the available posts of the CPT "Headers".

With the following code it is possible for me to get the selected Post/ID:

$value = get_field( "header-selection" );

Now I want to integrate the ID in the following code:

$post = get_post(18);
$the_content = apply_filters('the_content', $post->post_content);
if ( !empty($the_content) ) {
  echo $the_content;
}

Instead of the Post ID "18", I would like to have the selection of the ACF field "header-selection".

I am sorry if I get things wrong. Unfortunately I have problems understanding PHP. I would be very grateful for a suggested solution or an idea.


Solution

  • Save the ACF value to a variable than use that variable in-place of the hardcoded id like below:

    $header_selection_id = get_field( "header-selection" );
    $post = get_post($header_selection_id);