bbp_insert_topic

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

Description

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

Parameters (2)

0. $topic_data — Optional. (array) => array()
The topic data.
1. $topic_meta — Optional. (array) => array()
The topic meta.

Usage

  1. if ( !function_exists( 'bbp_insert_topic' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/topics/functions.php'; 
  3.  
  4. // The topic data. 
  5. $topic_data = array(); 
  6.  
  7. // The topic meta. 
  8. $topic_meta = array(); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bbp_insert_topic($topic_data, $topic_meta); 
  12.  

Defined (1)

The function is defined in the following location(s).

/includes/topics/functions.php  
  1. function bbp_insert_topic( $topic_data = array(), $topic_meta = array() ) { 
  2.  
  3. // Parse arguments against default values 
  4. $topic_data = bbp_parse_args( $topic_data, array( 
  5. 'post_parent' => 0, // forum ID 
  6. 'post_status' => bbp_get_public_status_id(),  
  7. 'post_type' => bbp_get_topic_post_type(),  
  8. 'post_author' => bbp_get_current_user_id(),  
  9. 'post_password' => '',  
  10. 'post_content' => '',  
  11. 'post_title' => '',  
  12. 'comment_status' => 'closed',  
  13. 'menu_order' => 0,  
  14. ), 'insert_topic' ); 
  15.  
  16. // Insert topic 
  17. $topic_id = wp_insert_post( $topic_data ); 
  18.  
  19. // Bail if no topic was added 
  20. if ( empty( $topic_id ) ) 
  21. return false; 
  22.  
  23. // Parse arguments against default values 
  24. $topic_meta = bbp_parse_args( $topic_meta, array( 
  25. 'author_ip' => bbp_current_author_ip(),  
  26. 'forum_id' => 0,  
  27. 'topic_id' => $topic_id,  
  28. 'voice_count' => 1,  
  29. 'reply_count' => 0,  
  30. 'reply_count_hidden' => 0,  
  31. 'last_reply_id' => 0,  
  32. 'last_active_id' => $topic_id,  
  33. 'last_active_time' => get_post_field( 'post_date', $topic_id, 'db' ),  
  34. ), 'insert_topic_meta' ); 
  35.  
  36. // Insert topic meta 
  37. foreach ( $topic_meta as $meta_key => $meta_value ) { 
  38. update_post_meta( $topic_id, '_bbp_' . $meta_key, $meta_value ); 
  39.  
  40. // Update the forum 
  41. $forum_id = bbp_get_topic_forum_id( $topic_id ); 
  42. if ( !empty( $forum_id ) ) { 
  43. bbp_update_forum( array( 'forum_id' => $forum_id ) ); 
  44.  
  45. // Return new topic ID 
  46. return $topic_id;