Support

Account

Home Forums Front-end Issues Relationship field in BBpress forum post

Solving

Relationship field in BBpress forum post

  • I’m trying to add a custom field to a BBpress forum post (or any standard form really).

    I have the code which inserts the ACF form field into the form and that works fine.

    I just need to save the value when the form is submitted. I have an action that taps into the forum post being saved… I just need to save the relationship field with it.

    I’ve tried….
    update_post_meta( $topic_id, 'field_57bf4e3cd6223', $_POST['acf']'field_57bf4e3cd6223'] );
    …and also…
    update_field( 'field_57bf4e3cd6223', $_POST['acf']['field_57bf4e3cd6223'] $topic_id );
    … where $topic_id is the forum topic ID but neither of those work. Any ideas here?

  • Okay… if anyone else has this issue… I have it working. Here is the code for adding a relationship field to a bbPress topic, saving/updating the value, and spitting it out on the front end. This allows the user to choose a related piece of content (such as a blog post) to show along with their topic post.

    Note: I set the relationship field to just return the post ID. If you want to return the post object, you’d need to modify this.

    // Inserts the field group into the bbPress form
    add_action ( 'bbp_theme_after_topic_form_content', 'bbp_extra_fields');
    function bbp_extra_fields() {
    	$options = array(
    		'field_groups' => array(123456), // replace with your ID
    		'form' => false
    	);
    	acf_form($options);
    }
    
    // This saves/updates the value when the topic is saved
    add_action ( 'bbp_new_topic', 'bbp_save_extra_fields', 10, 1 );
    add_action ( 'bbp_edit_topic', 'bbp_save_extra_fields', 10, 1 );
    function bbp_save_extra_fields($topic_id=0) {
    	if (isset($_POST) && is_array($_POST['acf']['field_123456789'])) {
    		$value = $_POST['acf']['field_123456789'];
    	   	update_field( 'field_123456789', $value, $topic_id );
    	}
    }
    
    // This spits the related content out as a link on the front end, after the topic content
    add_action('bbp_theme_after_reply_content', 'bbp_show_extra_fields');
    function bbp_show_extra_fields() {
    	$topic_id = bbp_get_topic_id();
    
    	if ( function_exists('get_field') && get_field('related_field_name', $topic_id) ) { 
    		$related_posts = get_field('related_field_name', $topic_id); 
    
    	    foreach ($related_posts as $related_post) {
    			if (get_post_status( $related_post) == 'publish') {
    	        	echo '<a href="' . get_permalink( $related_post) . '" class="btn">' . get_the_title( $related_post) . '</a>';
    			}
    	    } 
    	}
    }
  • Thanks @jaybuys for posting your solution, it will probably help someone else in the future.

  • Can this idea be used somehow to replace this plugin ?
    (4 year old and we can freely say non existing.)

    https://wordpress.org/plugins/bbpress-post-topics/screenshots/

  • @jaybuys I noticed you have this working on topics. Do you have any advice on trying to get a relationship field to display on a bbpress profile page.

    EX:
    I am allowing users to relate posts to their profile, and want to display those on their profile pages. I have the field in place using ACF but displaying it on the page is where I am lost.

  • Solved my own question playing off @jaybuys. Since the relationship field on all user edit screens (wp/bbp) all we need to do is call it on the profile page. I have hooked into the after the user profile and am showing the related posts.

    add_action('bbp_template_after_user_profile', 'bbp_show_extra_fields');
    function bbp_show_extra_fields() {
    	$topic_id = get_user_by( 'slug', get_query_var( 'author_name' ) );
    
    	if ( function_exists('get_field') && get_field('relationship_field', $topic_id) ) {
    		$related_posts = get_field('relationship_field', $topic_id);
    
    	    foreach ($related_posts as $related_post) {
    			if (get_post_status( $related_post) == 'publish') {
    	        	echo '<a href="' . get_permalink( $related_post) . '" class="btn">' . get_the_title( $related_post) . '</a>';
    			}
    	    }
    	}
    }
Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Relationship field in BBpress forum post’ is closed to new replies.