• I am creating a webpage which has both lots of pages, and blog posts. I am trying to get the search results page to display first all results for pages, and then for the posts.

    Is this possible with just tampering with the “loop-search.php” which I am using in my child theme, or with the files for Relevanssi?

    And if not, I’ve allready found a code that includes two custom WP_QUERY loops. This works well, but then the Relevanssi plugin doesnt work. Is it possible to get relevanssi working again, by somehow hooking the new loops up with the Relevanssi plugin?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Relevanssi doesn’t work with those custom query loops.

    Use ‘relevanssi_hits_filter’. It lets you filter search results, so you can sort them pages first and posts then, and you can just split the loop after you’ve run through all the pages.

    See this thread and this knowledge base entry.

    Thread Starter andre77

    (@andre77)

    Hi msaari!

    Thanks for your reply! I checked out the links you provided, and it really sounds like the answer I was hoping for! However I am still in the process of trying to get a grip of php, so I am bit lost when it comes to putting this solution into practice…

    Could you give me an example code on how I would get the search result, to first display all hits for pages, and then for blog posts?

    Plugin Author Mikko Saari

    (@msaari)

    Sorry, but I don’t have any example code ready and writing some is a bit more involved task than I have time for at the moment.

    You need to add a filter that goes through the search results array and divides the results to two arrays: one for posts, one for pages. Then they are put back together, pages first, posts then.

    That’ll sort the pages first, posts then. If you want to make that separation visible on your search results page, you need to check for $post->post_type in your search results loop and when that changes from page to post, you know the type has changed.

    Thread Starter andre77

    (@andre77)

    Ok msaari!

    But would that allow me to display headers prior to post-type. For example: “Search results for pages” and further down “Search results for blog posts”?

    Is there someone else around here, that has done similar thing in their project, that can provide me some code?

    Plugin Author Mikko Saari

    (@msaari)

    Yes, it would – just print out “Search results for pages” before the loop and “Search results for posts” when you notice the change.

    Thread Starter andre77

    (@andre77)

    Hey again msaari!

    I recently got a hold of a php-programmer, so now I have reasonable hope that we will figure this thing out. But we still have some questions:

    In what page would you use the ‘relevanssi_hits_filter? Would you use it in search.php? In the code you supplied there is an add_filter call and its belonging function, but where would you make the add_filter call?

    How do I access the search results array? In search.php they are only displayed with:
    get_template_part( ‘loop’, ‘search’ );

    Regards,
    André

    Plugin Author Mikko Saari

    (@msaari)

    You can place the code anywhere, but functions.php of your theme is the standard location. The add_filter() call needs to be done somewhere where it will be executed always, and functions.php is good for that.

    The search results array is passed as a variable to the function called with the relevanssi_hits_filter. See Relevanssi knowledge base for an example: https://www.relevanssi.com/knowledge-base/relevanssi_hits_filter/ (looks like the server’s down at the moment, though).

    Thread Starter andre77

    (@andre77)

    Thanks for your help msaari! We got it to work now ??

    Here is the code we used, if someone else is looking for the same thing:

    Put these modification in the “loop.php”-file:

    Before the loop starts:

    $previous_post_type = 'page'; //Sets the parameter for the first post that will turn up in the search result

    You also would put in a header before the loop starts that says something like:


    <h3>Search results for pages</h3>

    If you are using the default WordPress theme “twentyten”, find the line where it says “/* How to display all other posts. */. and modify the code as below:


    ...
    <?php else : ?>
    <?php if ( $previous_post_type == 'page' && $post->post_type == 'post') echo '<h3>Search results for blog posts</h3>'; ?>
    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>
    <h2 class="entry-title">
    ...

    The post type is then checked at the end of the loop, to see if it has changed from page to post. Put this code right before “endwhile”:


    <?php $previous_post_type = $post->post_type;?>
    <?php endwhile; // End the loop. Whew. ?>

    And this code goes into functions.php:


    /* Relevansi hit filter - customization of search results: */
    add_filter( 'relevanssi_hits_filter', 'my_hits_filter' );

    function my_hits_filter( $data )
    {
    $hits = $data[0];
    $pages = array();
    $posts = array();
    foreach ( $hits as $key => $hit )
    {
    if ( 'page' == $hit->post_type )
    {
    $pages[] = $hit;
    }
    else if ('post' == $hit->post_type )
    {
    $posts[] = $hit;
    }
    }

    $filtered_hits = array_merge($pages, $posts);
    $filtered_data = array( $filtered_hits, $data[1] );
    return $filtered_data;
    }

    Plugin Author Mikko Saari

    (@msaari)

    Great, glad to hear you got it working. Probably better to put the code in a Pastebin before the forum mods remove it…

    I could actually put in a knowledge base article on the Relevanssi web site. If you’ve got a web site I could link to, I’d be glad to do that.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to separate page and post search results?’ is closed to new replies.