Support

Account

Home Forums Backend Issues (wp-admin) Trouble Getting Post Content in Relationship Result

Solved

Trouble Getting Post Content in Relationship Result

  • I’m having trouble getting the content in the relationship result. I can load custom fields and the title but get_the_content or the_content are failing. Am I going about this wrong? Appreciate any feedback!

    
    // Modify ACF Relationship field list
    add_filter('acf/fields/relationship/result/name=wod_stat_wod', 'ws_relationship_result', 10, 4);
    function ws_relationship_result($title, $post, $field, $post_id){
        // load a custom field from this $object and show it in the $result
        $wod_name = get_field('wod_name', $post->ID);
    	$content = apply_filters( 'the_content', get_the_content($post->ID) );
    	if ( !empty($wod_name) ) {
    		$title = $title . ' - ' . $wod_name;
    	} else {
    		$title = $title;
    	}
        return $title . '<br />' . get_the_content($post->ID) . '<br /><a href="' . get_permalink($post->ID) .'">View WOD</a>';
    }
    
  • Hi @inhouse

    That’s because you can’t pass the post ID to the get_the_content() or the_content() function. Please check this page to learn more about it: https://codex.wordpress.org/Function_Reference/get_the_content.

    If you want to get the content, you need to use the get_post() function instead. This page should give you more idea about it: http://wordpress.stackexchange.com/questions/9667/get-wordpress-post-content-by-post-id.

    I hope this helps 🙂

  • Oh, of course! I figured it had to be something obvious. Thanks so much

  • This did the trick for me:

    
    global $post;
    
    $related_posts = get_field( 'related_posts' );
    
    if( $related_posts ) : 
        foreach( $related_posts as $post ) : setup_postdata( $post );
    
            // This worked for me
            $output = apply_filters( 'the_content', $post->post_content );
            echo $post->post_content;
            
        endforeach;
        wp_reset_postdata();
    endif;
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Trouble Getting Post Content in Relationship Result’ is closed to new replies.