Support

Account

Home Forums ACF PRO Get the post id from user selection using relationship?

Solved

Get the post id from user selection using relationship?

  • How can I get the post id’s from a selection of post from the field type Relationship?

    I am using the relationship field type asking the user to select 3 posts. My out put is displaying this:
    array(3) { [0]=> int(5623) [1]=> int(629) [2]=> int(625) }

    I want to simply output the id’s comma speparated like this:

    5623, 629, 625

    I was able to do this with taxonomy terms but I can not figure out how to format the output to display only the id’s comma separated when using the relationship field type.

  • How are you getting the value of the field to be displayed and how are you displaying it? Can you post your code for this?

  • I have the relationship field set to use the Post ID.

    Here’s my code where I need the variable “$theids” at line 8 to output the post id’s:

    <?php 
    $theids = get_field(post_id_one);
    $latest_title = get_field(latestposts_title);
    if( get_field('show_posts') )
    {
        echo '<div class="container"><div class="acsc-outer">
    <div class="fancyheader"><span class="acsc-title">' . $latest_title . '</span></div>
    </div>' . do_shortcode( '[display-posts include_excerpt="true" id="'.$theids.'"]' ) . '</div>';
    }
    ?>
  • $theids will be returned as an array and it’s being converted to a string.

    Change the shortcode call to

    do_shortcode( '[display-posts include_excerpt="true" id="'.implode(', ', $theids).'"]' )

  • Thanks John, it’s working. Very much appreciated.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Get the post id from user selection using relationship?’ is closed to new replies.