Support

Account

Home Forums General Issues Pull in \"related\" posts excerpt?

Solving

Pull in \"related\" posts excerpt?

  • The CPT “Stories” has “Posts” related to it. I named the relationship “related_posts”.

    When I do a vardump, it looks like get_field (‘related_posts’) will pull in the Author, Date, and Post_Content.

    How do I pull in the excerpt and Featured Image as well?

  • Post excerpt should be in the data returned if a custom excerpt is entered. If content is not entered in the field then you need to furn get_the_excerpt() when looping over the posts.

    To get the featured image you need to do this same way that you would when looping over the posts as well.

    WP does not return these fields when getting a post as part of the post data.

    If you want them as part of the values that acf returns the you need to create an acf/load_value filter, priority > 10, and in the filter loop over all of the posts and populate these things yourself.

  • Sorry, that should have been acf/format_value filter. Looking at your other questions I will expand on it.

    
    add_filter('acf/load_value/type=relationship', 'get_extra_post_data', 20, 3);
    function get_extra_post_data($value, $post_id, $field) {
      if (empty($value)) {
        // no value to filter
        return $value;
      }
      // this is needed because we will be working with excerpts
      global $post;
      foreach ($value as $post) {
        $post->excerpt = get_post_excerpt();
        $post->image = get_post_thumbnail();
      } // end foreach post
      // reset post data because we messed with the global
      wp_reset_postdata()
      return $value
    }
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.