Support

Account

Home Forums ACF PRO Getting post titles from serialized array of post IDs

Solved

Getting post titles from serialized array of post IDs

  • I have a relationship field of post titles that appears on a custom post type…..when this custom post type is entered, one or more of the related posts is selected. The post is stored as a “post object”, not “post ID”.

    In the database for this (custom) post, this gives me a metadata field with an array that looks like this:

    a:8:{i:0;s:4:”1423″;i:1;s:4:”1424″;i:2;s:4:”1425″;i:3;s:4:”1422″;i:4;s:4:”1427″;i:5;s:4:”1428″;i:6;s:4:”1430″;i:7;s:4:”1426″;}

    Where “1423”, “1424”, etc. are the Post IDs for which I want to return the post titles (as a comma delimited, unlinked list, e.g. “Post 1, Post 2, Post 3,..”

    I’ve tried what I used in the past but it isn’t working, I can get a list of the IDs, or the title of the last related post added, but not a list of all the titles…..gah!

    Here’s the code I’ve tried:

    $values = get_post_meta($postID,'applicable_resort',true); 
    foreach ($values as $value) { echo $value.', '; } 

    $postID is the ID of the original (custom) post held in a variable because before I get to this field there is a loop that sets up the postdata from the related post that uses the $post variable (including $post->ID) so I could not reuse that. That’s needed to pull tons of other metadata from that related post.

    BUT this snippet just gives me a comma separated list of the post IDs.

    I’ve tried using “get_field” instead of get_post_meta (didn’t work) and also setting up the post title as a variable after establishing $myvalues, like this:

    $myvalues = get_post_meta($postID,'applicable_resort',true); 
    foreach ($myvalues as $myvalue) { $resortname = get_the_title($myvalue->ID); echo $resortname.', '; 

    That just returns the title of the last related post added, not all of them.

    I’ve also tried just using “the_field()” but that doesn’t work, it gives me nothing at all.

    What am I doing wrong? I’m not a coder, just fairly good at following instructions and modifying code I find here an other support forums…..but just totally lost on this one.

  • You should be able to do this

    
    $values = get_post_meta($postID,'applicable_resort',true); 
    foreach ($values as $value) { echo get_the_title($value).', '; }
    

    Let me know if that doesn’t work

  • Thank you John! Once again, you’ve saved my butt!

    That works perfectly.

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

The topic ‘Getting post titles from serialized array of post IDs’ is closed to new replies.