ACF Relationship field in discourse_publish_format_html

Any ideas on how to publish a WP post that includes ACF relationship fields?

Is iFrame my best bet?

1 Like

Hey Richard,

Almost never :wink: And not in this case.

Could you elaborate on the need here a bit more, and also what you’ve tried so far?

As the title of your topic notes, you can use the discourse_publish_format_html filter to change the html of a post before it is sent to discourse. There’s also some good examples on how to use ACF relationship fields in the ACF documentation. These could be appled along with the discourse_publish_format_html filter.

2 Likes

I have two CPTs Memes and People. Each has an ACF relationship field called meme_person. A meme could have several related people and the meme also has a comment text field. The name of each person is the title of the People post.

I want to show the meme image on Discoures, followed by the comment and a list of the related people.

Here is my current attempt which I haven’t tested because my foreach line is a wild-assed guess.

Is this the right format?

// when publishing a post to Discourse

function rs_custom_publish_format_html( $output, $post_id ) {

	$post = get_post( $post_id );
	$type = get_post_type($post_id);
	
	// if featured image is present, use it
	if ( has_post_thumbnail( $post_id ) ) {
		$image = "{thumbnail}<br><br>";
	} else {
		$image = "";
	}

    // if post is a meme, get the custom field information
    if ( 'memes' === $type) {
    	$comment = get_post_meta($post->ID, '_comment', true);
		$people = ''
		$meme_person = get_field('meme_person');
		if( $meme_person ):
			foreach( $meme_person as $people ):
				$title = get_the_title( $featured_post->ID );
				$people .= $title ?> . ', '
			<?php endforeach; ?>
			</ul>
		<?php endif; ?>

        <?php ob_start(); ?>
  
        <small>Originally published at: {blogurl}</small><br><br>
        <?php echo $image ?>
        <?php echo $comment ?><br>
        <?php echo $people ?>
        {excerpt}
	  
        <?php 
        $output = ob_get_clean();
        
        return $output;
    }
      
    // else return
    ob_start();
    the_permalink( $post_id );
    $output = ob_get_clean();
    return $output;
    
}
add_filter( 'discourse_publish_format_html', 'rs_custom_publish_format_html', 10, 2  );
1 Like

Sorry to be dense, but I’m not quite sure if you’re asking me to review all of your code or just the use of the Discourse filter. It’s the right format in the sense that the filter expects you to modify the $output, and use the template tags to interpolate values. For more on that see this topic

2 Likes

Thanks. That topic states: The template has the following template tags available to it: {excerpt}, {blogurl}, {author}, {thumbnail}, {featuredimage}

On first reading, I thought those were the only elements that could be passed, which is why I originally asked about iFrame. Some other threads suggested that the ability to search by Post ID for other meta fields was now available. (Which perhaps should be mentioned in the Template customization thread)

Your original response to this thread confirmed the ability to search across relationship fields and you asked what I’ve tried so far.

The code I uploaded is what I have so far, and I wondered if you have any insight on the foreach line since that’s the only part of the relationship field search that stymies me. If not, I’ll search elsewhere and post back here.

Cheers.

1 Like

For anyone who wants to expand an ACF relationship field, here’s what finally worked when the CPT I’m sending to Discourse is called memes which includes a relationship field (as object) called meme_person which links to another CPT called people

function rs_custom_publish_format_html( $output, $post_id ) {

	$post = get_post( $post_id );
	$type = get_post_type($post_id);
	
	// if featured image is present, use it
	if ( has_post_thumbnail( $post_id ) ) {
		$image = "{thumbnail}<br><br>";
	} else {
		$image = "";
	}

    // if post is a meme, get the custom field information
    if ( 'memes' === $post->post_type) {
    	$comment = get_post_meta($post->ID, 'comment', true);
		$peopleout = ' ';

		$persons = get_post_meta($post->ID, 'meme_person', true);
		if( $persons ):
			foreach( $persons as $person ):
		        $title = get_the_title($person);
				$peopleout .= $title . ' ' ;
			endforeach;
		endif; ?>

        <?php ob_start(); ?>
  
        <small>Originally published at: {blogurl}</small><br><br>
        <?php echo $image ?>
        <?php echo $comment ?><br>
        <?php echo $peopleout ?>

        {excerpt}
	  
        <?php 
        $output = ob_get_clean();
        
        return $output;
    }
  
      
    // else return
    ob_start();
    the_permalink( $post_id );
	$output = ob_get_clean();
	return $output;
    
}
add_filter( 'discourse_publish_format_html', 'rs_custom_publish_format_html', 10, 2  );

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.