Skip to:
Content
Pages
Categories
Search
Top
Bottom

Paginated search results

  • I’m looking for advice on how to modify the search functionality to paginate the results that are returned; i.e. the default functionality returns N matches in each of the following categories: thread title, recent posts and relevant posts, but I would like to allow a user to scroll through repeated pages of search results.

    I’ve had a look at search.php and this is structured around returning a fixed number of results for each query, which effectively hides anything that doesn’t come up in the first N results of the search. The concept of listing the results of 3 different queries in one results page also seems to be incompatible with providing a pagination mechanism (which list do you paginate?). I feel that the default method of handling search results is a bit limited and confusing for users familiar with a typical Google-style search that returns one simple list in multiple pages.

    I have had a look at the code, but I’m no PHP guru; is there any way of getting the sort of results I would like without fundamentally rewriting the search mechanism?

    p.s. not being overly critical here – I think bbPress is a great piece of work!

Viewing 3 replies - 1 through 3 (of 3 total)

  • chrishajer
    Participant

    @chrishajer

    I was not happy with bbPress search functionality a long time ago, now I just live with it. The results were always weird looking to me.

    Anyway, now I just use Google to search my site if I want to find anything. Something like:

    site:mysite.com term1 term2 at google.com does the trick. Of course, the content needs to be indexed at Google for that to work. An alternative I heard about was Google Custom Search Engine:

    http://www.google.com/coop/cse/

    I have no idea how that works but it’s an option.

    I think doing what you propose with the search results would require a lot of work (having 3 sets of search results complicates it in my mind), but maybe one of the plugin writers will show an easy way to do what you propose. Maybe you could perform all three searches and aggregate the results, then paginate them. That would be easier than breaking them into title matches, recent posts and relevant posts. That begs for having advanced search options anyway. Like, why can’t you search for a user’s name? I thought you used to be able to, but searching my forum now for a known user name doesn’t return anything like “User Name matches”: I thought it used to.

    Looking at how a user profile works is interesting. If you click on a user’s name to get to profile.php?id=the_user_id, you’ll see a paginated list of all posts made by that user ordered by the last time they replied (so, that user’s posts in the order that they made them, most recent first) and a list of threads started, I think. Maybe a plugin could be written to replace the search queries with something like what profile.php does, except instead of selecting all posts and threads for a user (which are returned in $posts and $threads) maybe you could do a similar query with your search term, in all the fields you want to check, then return that? Hmmmm…

    I think search is a weak point for lots of blogging platforms and forum software, and I think getting it right is important, so it’s worth looking into.

    Good luck.

    (sorry: edited version substantially)

    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.

    Thanks for the replies (haven’t been able to work on this for a few weeks, hence my late response). As I said previously, I’m not keen on the 3 list approach at all, because I think most people expect a search function to return a single list of results (regardless of the strategy used to generate the results). Search is particularly important for the site I am working on, because the forum will be used to store race results, and users want to be able to search through several years of past results using an event name, for example.

    I have used Google site search on an existing website. I wasn’t aware of the custom search engine, and it looks interesting but I would like to keep the search more tightly integrated with the site if possible. The Google solution also appears to be JS dependent for displaying results in an i-frame.

    I think that modifying the relevant post search to provide pagination, as suggested, might be an acceptable solution. I’m not sure that I require the title and recent post searches, so I could simply discard these and go for a single list solution.

    Thanks again for the advice.

Viewing 3 replies - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.