bbp_parse_query

Add checks for bbPress conditions to parse_query action.

Description

bbp_parse_query( (WP_Query) $posts_query ); 

If it's a user page, WP_Query::bbp_is_single_user is set to true. If it's a user edit page, WP_Query::bbp_is_single_user_edit is set to true and the the wp-admin/includes/user.php file is included. In addition, on user/user edit pages, WP_Query::home is set to false & query vars bbp_user_id with the displayed user id and author_name with the displayed user's nicename are added.

If it's a forum edit, WP_Query::bbp_is_forum_edit is set to true If it's a topic edit, WP_Query::bbp_is_topic_edit is set to true If it's a reply edit, WP_Query::bbp_is_reply_edit is set to true.

If it's a view page, WP_Query::bbp_is_view is set to true If it's a search page, WP_Query::bbp_is_search is set to true

Parameters (1)

0. $posts_query (WP_Query)
The posts query.

Usage

  1. if ( !function_exists( 'bbp_parse_query' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/core/template-functions.php'; 
  3.  
  4. // The posts query. 
  5. $posts_query = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bbp_parse_query($posts_query); 
  9.  

Defined (1)

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

/includes/core/template-functions.php  
  1. function bbp_parse_query( $posts_query ) { 
  2.  
  3. // Bail if $posts_query is not the main loop 
  4. if ( ! $posts_query->is_main_query() ) 
  5. return; 
  6.  
  7. // Bail if filters are suppressed on this query 
  8. if ( true === $posts_query->get( 'suppress_filters' ) ) 
  9. return; 
  10.  
  11. // Bail if in admin 
  12. if ( is_admin() ) 
  13. return; 
  14.  
  15. // Get query variables 
  16. $bbp_view = $posts_query->get( bbp_get_view_rewrite_id() ); 
  17. $bbp_user = $posts_query->get( bbp_get_user_rewrite_id() ); 
  18. $is_edit = $posts_query->get( bbp_get_edit_rewrite_id() ); 
  19.  
  20. // It is a user page - We'll also check if it is user edit 
  21. if ( !empty( $bbp_user ) ) { 
  22.  
  23. /** Find User *********************************************************/ 
  24.  
  25. // Setup the default user variable 
  26. $the_user = false; 
  27.  
  28. // If using pretty permalinks, always use slug 
  29. if ( get_option( 'permalink_structure' ) ) { 
  30. $the_user = get_user_by( 'slug', $bbp_user ); 
  31.  
  32. // If not using pretty permalinks, always use numeric ID 
  33. } elseif ( is_numeric( $bbp_user ) ) { 
  34. $the_user = get_user_by( 'id', $bbp_user ); 
  35.  
  36. // 404 and bail if user does not have a profile 
  37. if ( empty( $the_user->ID ) || ! bbp_user_has_profile( $the_user->ID ) ) { 
  38. $posts_query->set_404(); 
  39. return; 
  40.  
  41. /** User Exists *******************************************************/ 
  42.  
  43. $is_favs = $posts_query->get( bbp_get_user_favorites_rewrite_id() ); 
  44. $is_subs = $posts_query->get( bbp_get_user_subscriptions_rewrite_id() ); 
  45. $is_topics = $posts_query->get( bbp_get_user_topics_rewrite_id() ); 
  46. $is_replies = $posts_query->get( bbp_get_user_replies_rewrite_id() ); 
  47.  
  48. // View or edit? 
  49. if ( !empty( $is_edit ) ) { 
  50.  
  51. // We are editing a profile 
  52. $posts_query->bbp_is_single_user_edit = true; 
  53.  
  54. // Load the core WordPress contact methods 
  55. if ( !function_exists( '_wp_get_user_contactmethods' ) ) { 
  56. include_once( ABSPATH . 'wp-includes/registration.php' ); 
  57.  
  58. // Load the edit_user functions 
  59. if ( !function_exists( 'edit_user' ) ) { 
  60. require_once( ABSPATH . 'wp-admin/includes/user.php' ); 
  61.  
  62. // Load the grant/revoke super admin functions 
  63. if ( is_multisite() && !function_exists( 'revoke_super_admin' ) ) { 
  64. require_once( ABSPATH . 'wp-admin/includes/ms.php' ); 
  65.  
  66. // Editing a user 
  67. $posts_query->bbp_is_edit = true; 
  68.  
  69. // User favorites 
  70. } elseif ( ! empty( $is_favs ) ) { 
  71. $posts_query->bbp_is_single_user_favs = true; 
  72.  
  73. // User subscriptions 
  74. } elseif ( ! empty( $is_subs ) ) { 
  75. $posts_query->bbp_is_single_user_subs = true; 
  76.  
  77. // User topics 
  78. } elseif ( ! empty( $is_topics ) ) { 
  79. $posts_query->bbp_is_single_user_topics = true; 
  80.  
  81. // User topics 
  82. } elseif ( ! empty( $is_replies ) ) { 
  83. $posts_query->bbp_is_single_user_replies = true; 
  84.  
  85. // User profile 
  86. } else { 
  87. $posts_query->bbp_is_single_user_profile = true; 
  88.  
  89. // Looking at a single user 
  90. $posts_query->bbp_is_single_user = true; 
  91.  
  92. // Make sure 404 is not set 
  93. $posts_query->is_404 = false; 
  94.  
  95. // Correct is_home variable 
  96. $posts_query->is_home = false; 
  97.  
  98. // User is looking at their own profile 
  99. if ( get_current_user_id() === $the_user->ID ) { 
  100. $posts_query->bbp_is_single_user_home = true; 
  101.  
  102. // Set bbp_user_id for future reference 
  103. $posts_query->set( 'bbp_user_id', $the_user->ID ); 
  104.  
  105. // Set author_name as current user's nicename to get correct posts 
  106. $posts_query->set( 'author_name', $the_user->user_nicename ); 
  107.  
  108. // Set the displayed user global to this user 
  109. bbpress)->displayed_user = $the_user; 
  110.  
  111. // View Page 
  112. } elseif ( !empty( $bbp_view ) ) { 
  113.  
  114. // Check if the view exists by checking if there are query args are set 
  115. $view_args = bbp_get_view_query_args( $bbp_view ); 
  116.  
  117. // Bail if view args is false (view isn't registered) 
  118. if ( false === $view_args ) { 
  119. $posts_query->set_404(); 
  120. return; 
  121.  
  122. // Correct is_home variable 
  123. $posts_query->is_home = false; 
  124.  
  125. // We are in a custom topic view 
  126. $posts_query->bbp_is_view = true; 
  127.  
  128. // Search Page 
  129. } elseif ( isset( $posts_query->query_vars[ bbp_get_search_rewrite_id() ] ) ) { 
  130.  
  131. // Check if there are search query args set 
  132. $search_terms = bbp_get_search_terms(); 
  133. if ( !empty( $search_terms ) ) 
  134. $posts_query->bbp_search_terms = $search_terms; 
  135.  
  136. // Correct is_home variable 
  137. $posts_query->is_home = false; 
  138.  
  139. // We are in a search query 
  140. $posts_query->bbp_is_search = true; 
  141.  
  142. // Forum/Topic/Reply Edit Page 
  143. } elseif ( !empty( $is_edit ) ) { 
  144.  
  145. // Get the post type from the main query loop 
  146. $post_type = $posts_query->get( 'post_type' ); 
  147.  
  148. // Check which post_type we are editing, if any 
  149. if ( !empty( $post_type ) ) { 
  150. switch( $post_type ) { 
  151.  
  152. // We are editing a forum 
  153. $posts_query->bbp_is_forum_edit = true; 
  154. $posts_query->bbp_is_edit = true; 
  155. break; 
  156.  
  157. // We are editing a topic 
  158. $posts_query->bbp_is_topic_edit = true; 
  159. $posts_query->bbp_is_edit = true; 
  160. break; 
  161.  
  162. // We are editing a reply 
  163. $posts_query->bbp_is_reply_edit = true; 
  164. $posts_query->bbp_is_edit = true; 
  165. break; 
  166.  
  167. // We are editing a topic tag 
  168. } elseif ( bbp_is_topic_tag() ) { 
  169. $posts_query->bbp_is_topic_tag_edit = true; 
  170. $posts_query->bbp_is_edit = true; 
  171.  
  172. // We save post revisions on our own 
  173. remove_action( 'pre_post_update', 'wp_save_post_revision' ); 
  174.  
  175. // Topic tag page 
  176. } elseif ( bbp_is_topic_tag() ) { 
  177. $posts_query->set( 'bbp_topic_tag', get_query_var( 'term' ) ); 
  178. $posts_query->set( 'post_type', bbp_get_topic_post_type() ); 
  179. $posts_query->set( 'posts_per_page', bbp_get_topics_per_page() ); 
  180.  
  181. // Do topics on forums root 
  182. } elseif ( is_post_type_archive( array( bbp_get_forum_post_type(), bbp_get_topic_post_type() ) ) && ( 'topics' === bbp_show_on_root() ) ) { 
  183. $posts_query->bbp_show_topics_on_root = true;