bbp_has_search_results

The main search loop.

Description

(object) bbp_has_search_results( (string) $args = '' ); 

WordPress does the heavy lifting.

Returns (object)

Multidimensional array of search information

Parameters (1)

0. $args — Optional. (string) => ''
The args.

Options

    ;        


    Usage

    1. if ( !function_exists( 'bbp_has_search_results' ) ) { 
    2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/search/template.php'; 
    3.  
    4. // The args. 
    5. $args = ''; 
    6.  
    7. // NOTICE! Understand what this does before running. 
    8. $result = bbp_has_search_results($args); 
    9.  

    Defined (1)

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

    /includes/search/template.php  
    1. function bbp_has_search_results( $args = '' ) { 
    2. global $wp_rewrite; 
    3.  
    4. /** Defaults **************************************************************/ 
    5.  
    6.  
    7. // Default query args 
    8. $default = array( 
    9. 'post_type' => $default_post_type, // Forums, topics, and replies 
    10. 'posts_per_page' => bbp_get_replies_per_page(), // This many 
    11. 'paged' => bbp_get_paged(), // On this page 
    12. 'orderby' => 'date', // Sorted by date 
    13. 'order' => 'DESC', // Most recent first 
    14. 'ignore_sticky_posts' => true, // Stickies not supported 
    15. 's' => bbp_get_search_terms(), // This is a search 
    16. ); 
    17.  
    18. // What are the default allowed statuses (based on user caps) 
    19. if ( bbp_get_view_all() ) { 
    20.  
    21. // Default view=all statuses 
    22. $post_statuses = array( 
    23. ); 
    24.  
    25. // Add support for private status 
    26. if ( current_user_can( 'read_private_topics' ) ) { 
    27. $post_statuses[] = bbp_get_private_status_id(); 
    28.  
    29. // Join post statuses together 
    30. $default['post_status'] = implode( ', ', $post_statuses ); 
    31.  
    32. // Lean on the 'perm' query var value of 'readable' to provide statuses 
    33. } else { 
    34. $default['perm'] = 'readable'; 
    35.  
    36. /** Setup *****************************************************************/ 
    37.  
    38. // Parse arguments against default values 
    39. $r = bbp_parse_args( $args, $default, 'has_search_results' ); 
    40.  
    41. // Get bbPress 
    42. $bbp =bbpress); 
    43.  
    44. // Call the query 
    45. if ( ! empty( $r['s'] ) ) { 
    46. $bbp->search_query = new WP_Query( $r ); 
    47.  
    48. // Add pagination values to query object 
    49. $bbp->search_query->posts_per_page = $r['posts_per_page']; 
    50. $bbp->search_query->paged = $r['paged']; 
    51.  
    52. // Never home, regardless of what parse_query says 
    53. $bbp->search_query->is_home = false; 
    54.  
    55. // Only add pagination is query returned results 
    56. if ( ! empty( $bbp->search_query->found_posts ) && ! empty( $bbp->search_query->posts_per_page ) ) { 
    57.  
    58. // Array of arguments to add after pagination links 
    59. $add_args = array(); 
    60.  
    61. // If pretty permalinks are enabled, make our pagination pretty 
    62. if ( $wp_rewrite->using_permalinks() ) { 
    63.  
    64. // Shortcode territory 
    65. if ( is_page() || is_single() ) { 
    66. $base = trailingslashit( get_permalink() ); 
    67.  
    68. // Default search location 
    69. } else { 
    70. $base = trailingslashit( bbp_get_search_results_url() ); 
    71.  
    72. // Add pagination base 
    73. $base = $base . user_trailingslashit( $wp_rewrite->pagination_base . '/%#%/' ); 
    74.  
    75. // Unpretty permalinks 
    76. } else { 
    77. $base = add_query_arg( 'paged', '%#%' ); 
    78.  
    79. // Add args 
    80. if ( bbp_get_view_all() ) { 
    81. $add_args['view'] = 'all'; 
    82.  
    83. // Add pagination to query object 
    84. $bbp->search_query->pagination_links = paginate_links( 
    85. apply_filters( 'bbp_search_results_pagination', array( 
    86. 'base' => $base,  
    87. 'format' => '',  
    88. 'total' => ceil( (int) $bbp->search_query->found_posts / (int) $r['posts_per_page'] ),  
    89. 'current' => (int) $bbp->search_query->paged,  
    90. 'prev_text' => is_rtl() ? '→' : '←',  
    91. 'next_text' => is_rtl() ? '←' : '→',  
    92. 'mid_size' => 1,  
    93. 'add_args' => $add_args,  
    94. ) ) 
    95. ); 
    96.  
    97. // Remove first page from pagination 
    98. if ( $wp_rewrite->using_permalinks() ) { 
    99. $bbp->search_query->pagination_links = str_replace( $wp_rewrite->pagination_base . '/1/', '', $bbp->search_query->pagination_links ); 
    100. } else { 
    101. $bbp->search_query->pagination_links = str_replace( '&paged=1', '', $bbp->search_query->pagination_links ); 
    102.  
    103. // Return object 
    104. return apply_filters( 'bbp_has_search_results', $bbp->search_query->have_posts(), $bbp->search_query );