I've looked at quite a few similar posts but no-one seems to be attempting the same thing as me here.

The search page is on the main wordpress site mainsite.com/s=the_query. The forum is at mainsite.com/forum/.

The custom functions:

 * Get forum DB connection
 * @return object
function connect_to_forum() {
    return new wpdb(DB_USER, DB_PASS, DB_NAME, DB_HOST);

 * Forum search function
 * @param int The default limit
 * @return array An array on stdClass objects
function forum_search($limit=20) {
    if ( is_search() ) {
        // Is there even a search query? i.e. ?s=THE_QUERY
        if ( isset($_GET['s'] ) ) {
            // Sanitize user input
            $query_var = sanitize_text_field( $_GET['s'] );

            // Generate string separated by space and plus i.e '+winning +games'
            $query_var = '+' . implode(' +', explode(' ', $query_var));

            $db = connect_to_forum();

            if ($db) {
                $rows = $db->get_results(
                        "SELECT title, item_date, message, discussion_id
                        FROM forum_search_index
                        WHERE MATCH (title) AGAINST (%s IN BOOLEAN MODE)
                        GROUP BY item_date
                        ORDER BY item_date DESC
                        LIMIT $limit"
                    , $query_var)
                // Close connection
                // Array of stdClass objects
                return $rows;
        return false;

...used on the search page thus:

$rows = forum_search(100);
if ($rows) {
   // do stuff...

Currently, the first column on the page contains the normal WP loop which shows WP search results. The second column contains the function above. Then I can foreach it etc... so I end up with two columns on the search page, wordpress site results one side, forum results the other. But shows ALL results which is also something that needs fixing.

What I need to do is merge these two sets of data somehow and display inside the standard WP loop (if possible). Would be nice to keep pagination working.

I looked at the pre_get_posts action but I'm a bit stuck on how to implement this. I can see it's possible to filter the query, but can you add items to it that are not WP_Post objects?

 * Modify search
function remove_pagination( $query ) {
    if ( $query->is_main_query() && is_search() ) {
        $query->query_vars['nopaging'] = 1;
        // run forum_search(), add results and sort perhaps?
        // although this looks like it only modifies the query
        // before returning the posts elsewhere
add_action( 'pre_get_posts', 'remove_pagination' );

This one was similar: Pagination broken when merging search results with additional WP_Query

...but was about adding additional items via WP_Query which isn't the same as in my case here.

Maybe if I could even just get the WP results as an array, and the forum results as an array, merge the two and sort by date or something straight in the template. Any suggestions on approaching this would be very helpful. Thanks.

== EDIT == I tried this and it is possible at least to merge the two:

        // on search.php
        $wp_results = $wp_query->posts;
        $forum_results = forum_search(100);
        $new_results = array_merge($wp_results, $forum_results);

        var_dump($new_results); // array(32) of stdClass/WP_Post objects

...probably not the most robust solution though (and pretty sure this will break pagination). Still interested in other ideas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.