Support

Account

Forum Replies Created

  • Thanks again John. That’s a good approach. I’ll give that a shot!

  • Thanks John. That’s kinda what I thought. I looked into doing it directly via the database but since the relationship field is serialized data it’s a bit tricky (though I’m sure not impossible).

    For anyone stumbling upon this, I was able to solve it by looping through and building an array of the info I need.

    I’m using “posts” and “authors” in place of “doctors” and “locations” but the idea is the same.

    $posts = get_posts(array(
    	'post_type' => 'post',
    	'posts_per_page' => -1,
    	'meta_key' => 'author',
    	'meta_value' => ' ',
    	'meta_compare' => '!='
    ));
    
    $authors_list = array();
    foreach ($posts as $post) {
    	$related_authors = get_field('author', $post->ID);
    	foreach ($related_authors as $author_id) {
    		if (!in_array($author_id, $authors_list)) {
    			$authors_list[] = $author_id;
    		}
    	}
    }
    
    function author_sort($a, $b) {
    	return strcmp($a->post_title, $b->post_title);
    }
    usort($authors_list, "author_sort");
    
    foreach ($authors_list as $author) {
    	echo '<a href="' . get_the_permalink($author_id) . '">' . get_the_title($author->ID) . '</a><br />';
    }
  • 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>';
    			}
    	    } 
    	}
    }
  • Terrific! Thank you!

    For anyone who may stumble on this thread… if you don’t require at least one selection to be made in your relationship fields then you may end up with a bunch of PHP warnings as it tries to check what are essentially null values. Just wrap this code in a conditional to make sure a value exists before looping through the items, like so…

    
    if ($value) {
    	foreach($value as $key => $id){
    		if( get_post_status( $id ) == 'publish' ){
    			$returned_value[] = $id;
    		}
    	}
    }
    
  • For anyone else stumbling across this post… I just had a very similar issue here. I wanted users with the ‘contributor’ role to be able to submit content and upload images. However, there are ways that you can add/extend capabilities to various users and roles through plugins like User Role Editor or in my case, just some simple code in my functions file…

    // Add upload capabilities to contributors
    function allow_contributor_uploads() {
    	if ( current_user_can('contributor') && !current_user_can('upload_files') ) {
    		$contributor = get_role('contributor');
    		$contributor->add_cap('upload_files');
    	}
    }
    add_action('admin_init','allow_contributor_uploads');
Viewing 5 posts - 1 through 5 (of 5 total)