Support

Account

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

Solved

Trouble Getting Post Content in Relationship Result

    • inhouse

    • September 24, 2016 at 12:40 pm

    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>';
    }
    
    • James

    • September 25, 2016 at 2:15 pm

    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 🙂

    • inhouse

    • September 26, 2016 at 1:36 am

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

    • addison

    • September 16, 2020 at 12:30 pm

    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)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.