Skip to:
Content
Pages
Categories
Search
Top
Bottom

Re: Paginated search results

@livibetter

Member

I made some changes to have paginated search results on relevant post. However, I am not sure is this what you need. You need to modify two files: search.php in root of bbPress and search.php of your template files.

Replace if block start at line 9 in search.php of root of bbPress with:

if ( $q = stripslashes( $q ) ) {
add_filter( 'bb_recent_search_fields', create_function( '$f', 'return $f . ", MAX(post_time) AS post_time";' ) );
add_filter( 'bb_recent_search_group_by', create_function( '', 'return "t.topic_id";' ) );
$bb_query_form->BB_Query_Form( 'post', array(), array( 'per_page' => 5, 'post_status' => 0, 'topic_status' => 0, 'post_text' => $q, 'forum_id', 'tag', 'topic_author', 'post_author' ), 'bb_recent_search' );
$recent = $bb_query_form->results;

$bb_query_form->BB_Query_Form( 'topic', array( 'search' => $q ), array( 'post_status' => 0, 'topic_status' => 0, 'search', 'forum_id', 'tag', 'topic_author', 'post_author', 'count' => 'found_rows' ), 'bb_relevant_search' );
$relevant = $bb_query_form->results;
global $relevant_round_rows;
$relevant_found_rows = $bb_query_form->found_rows;
$q = $bb_query_form->get( 'search' );
}

I added a query parameter 'count'=>'found_rows' to second $bb_query_form for retrieving total post count. And I added global $relevant_found_rows to pass to template search.php (you can use bb_load_template to do same thing).

Insert the following code just before second <?php endif; ?> from bottom in template search.php

<div class="nav">
<?php
$uri = $_SERVER['REQUEST_URI'];
if (false !== $pos = strpos($uri, '?')) {
$uri = substr($uri, 0, $pos) . '?q=' . urlencode($q) . '%_%';

if (!isset($page))
$page = 1;

global $relevant_found_rows;
echo paginate_links(array('base' => $uri, 'format' => '&page=%#%', 'total'=>ceil($relevant_found_rows/$bb->page_topics), 'current'=>$page));
}
?>
</div>

Should look like:

<?php if ( $relevant ) : ?>
<h2><?php _e('Relevant posts')?></h2>
<ol class="results">
<?php foreach ( $relevant as $bb_post ) : ?>
<li><h4><a href="<?php post_link(); ?>"><?php topic_title($bb_post->topic_id); ?></a></h4>
<?php echo bb_show_context($q, $bb_post->post_text); ?>

<small><?php _e('Posted') ?> <?php bb_post_time( __('F j, Y, h:i A') ); ?></small>

</li>
<?php endforeach; ?>
</ol>
[insert the code above here]
<?php endif; ?>

You can find paginate_links in bb-includes/wp-functions.php.

Skip to toolbar