Skip to:
Content
Pages
Categories
Search
Top
Bottom

Reply To: Create a new forum on each new post?

@egyptimhotep

Participant

I would like the first forum topic created to be displayed on the post page.

You need to add another line of code to action function. We need to save topic id to post meta.

whole function.php

// Add the hook action
add_action('transition_post_status', 'send_new_post', 10, 3);

// Listen for publishing of a new post
function send_new_post($new_status, $old_status, $post) {
  if('publish' === $new_status && 'publish' !== $old_status && $post->post_type === 'post') {
	
	$my_post = array(
	  'post_title' => $post->ID,
	);
	 
	$forum_id = bbp_insert_forum($my_post);

	$my_topic = array(
		'post_title' => $post->post_title,
		'post_content' => $post->post_content,
		'post_parent' => $forum_id
	);

	$topic_id = bbp_insert_topic($my_topic);

	update_post_meta( $post->ID, 'forum_first_topic', $topic_id );

  }
}

In single.php we can call your shortcode for showing topic by id because whe have post meta.

Paste this into loop:

$topic_id = get_post_meta( get_the_ID(), 'forum_first_topic', true );
echo do_shortcode(' [bbp-single-topic id='.$topic_id.']');

Whole single php loop:

<?php if ( have_posts() ) : ?>
	<?php while ( have_posts() ) : the_post(); ?>    
	
		<div class="article">
			<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>			
			<?php the_content(); ?>	
			<h2>Forum: <?php echo basename(get_permalink()); ?></h2>
			<?php 
				$post = get_post(get_the_ID());
				$mypost = get_page_by_title(get_the_ID(),OBJECT,'forum');
				$topic_id = get_post_meta( get_the_ID(), 'forum_first_topic', true );
				
				echo do_shortcode(' [bbp-single-topic id='.$topic_id.']');
				echo do_shortcode( '[bbp-single-forum id='.$mypost->ID.']' ); ?>
		</div>

	<?php endwhile; ?>
<?php endif; ?>
Skip to toolbar