I know, this question was asked before several times, but I didn't find the answer suitable for my task. I asked the same question on StackOverflow already, but, I guess, this community is more attuned to WordPress related questions.

I want to search in forum for the same keyword and then simply add results to existing WP search results array. In Joomla there's an event for it, which allows you to add custom searches by plugins. Only when all plugins were fired, pagination is calculated. WordPress seems to work in a very different way and it confusing me right now.

The main condition: it MUST work on main search.php, so custom search pages are not a solution.

I've added pre_get_posts hook to my plugin and queried forum by the same keyword. Then adjusted the look of forum results with the_permalink, author_link, the_author, the_category hooks. I even changed the number of posts found and pagination now shows proper number pf pages. No problem with that.

What I have problem with, is pagination behavior and querying of forum, when WordPress has no more posts matching criteria.

Long story short, forum results should be added to WP results and, ideally, sorted by given criteria (title, date, etc.).

Can anybody give me an example on where and when should I add my results properly? Honestly, I'm a little lost here.

A little update...

Joomla has a very simple algorithm in processing results from different plugins (queries). First it queries its own database, then calls all search related plugins and passes them current array of results. Each plugin simply pushes its own results to this array. And only then pagination is calculated. Easy-peasy.

Basically, I need the same: somehow push the results from forum to WP results array and make pagination work. I don't want forum to be searched from some widget and results displayed on other search page. This I know how to achieve.


I think, I found a solution by gmazapp. At least, it worked for me. It's a little hacky-ish, but does the job pretty good, so I'm sharing it, if someone will be looking for the same thing in the future.

| improve this answer | |
if ( have_posts() ) : ?>

    <header class="page-header">
        <h1 class="page-title"><?php 
            printf( __( 'Search Results for: %s', 'twentyfifteen' ), get_search_query() ); 
    </header><!-- .page-header -->

    // Start the loop.
    while ( have_posts() ) : the_post(); ?>

         * Run the loop for the search to output the results.
         * If you want to overload this in a child theme then include a file
         * called content-search.php and that will be used instead.
        get_template_part( 'content', 'search' );

    // End the loop.

    // Previous/next page navigation.
    the_posts_pagination( array(
        'prev_text'          => __( 'Previous page', 'twentyfifteen' ),
        'next_text'          => __( 'Next page', 'twentyfifteen' ),
        'before_page_number' => '<span class="meta-nav screen-reader-text">' . 
            __( 'Page', 'twentyfifteen' ) . ' </span>',
    ) );

// If no content, include the "No posts found" template.
else :
    get_template_part( 'content', 'none' );

| improve this answer | |
  • 4
    Please add an explanation to your answer: why could that solve the problem? What is the meaning of this code snippet? – fuxia May 27 '16 at 11:01
  • Well, I told above, that I need forum results to be included in search.php. As far as I understand, you simply provided contents of generic search page. With separate pagination. I need ONE search page, ONE pagination for all results - WP and forum combined. – The Krotek May 27 '16 at 12:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.