bbp_insert_topic( array $topic_data = array(), arrap $topic_meta = array() )

A wrapper for wp_insert_post() that also includes the necessary meta values for the topic to function properly.


Description Description


Parameters Parameters

$topic_data

(Optional) Forum post data

Default value: array()

$topic_meta

(Optional) Forum meta data

Default value: array()


Top ↑

Source Source

File: includes/topics/functions.php

function bbp_insert_topic( $topic_data = array(), $topic_meta = array() ) {

	// Parse arguments against default values
	$topic_data = bbp_parse_args( $topic_data, array(
		'post_parent'    => 0, // forum ID
		'post_status'    => bbp_get_public_status_id(),
		'post_type'      => bbp_get_topic_post_type(),
		'post_author'    => bbp_get_current_user_id(),
		'post_password'  => '',
		'post_content'   => '',
		'post_title'     => '',
		'comment_status' => 'closed',
		'menu_order'     => 0
	), 'insert_topic' );

	// Insert topic
	$topic_id = wp_insert_post( $topic_data, false );

	// Bail if no topic was added
	if ( empty( $topic_id ) ) {
		return false;
	}

	// Parse arguments against default values
	$topic_meta = bbp_parse_args( $topic_meta, array(
		'author_ip'          => bbp_current_author_ip(),
		'forum_id'           => 0,
		'topic_id'           => $topic_id,
		'voice_count'        => 1,
		'reply_count'        => 0,
		'reply_count_hidden' => 0,
		'last_reply_id'      => 0,
		'last_active_id'     => $topic_id,
		'last_active_time'   => get_post_field( 'post_date', $topic_id, 'db' )
	), 'insert_topic_meta' );

	// Insert topic meta
	foreach ( $topic_meta as $meta_key => $meta_value ) {

		// Prefix if not prefixed
		if ( '_bbp_' !== substr( $meta_key, 0, 5 ) ) {
			$meta_key = '_bbp_' . $meta_key;
		}

		// Update the meta
		update_post_meta( $topic_id, $meta_key, $meta_value );
	}

	// Update the topic and hierarchy
	bbp_update_topic( $topic_id, $topic_meta['forum_id'], array(), $topic_data['post_author'], false );

	/**
	 * Fires after topic has been inserted via `bbp_insert_topic`.
	 *
	 * @since 2.6.0 bbPress (r6036)
	 *
	 * @param int $topic_id               The topic id.
	 * @param int $topic_meta['forum_id'] The topic forum meta.
	 */
	do_action( 'bbp_insert_topic', (int) $topic_id, (int) $topic_meta['forum_id'] );

	// Return topic_id
	return $topic_id;
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.