• invest0r7

    (@invest0r7)


    I’d like to separate the search results by post type. Currently, they’re all listed in the same loop, with pages listing first, then posts listed after. But I’d like to have them separated in two tabs…a tab for page results, and another tab for post results.

    Is this something that anyone here has had success doing while also using Relevanssi?

    I tried using the solution from this Stackoverflow answer, but couldn’t get pagination to work right, nor could I get it to work with Relevanssi.

    I understand this may be outside of the scope of this plugin, but I would think this kind of thing would be so beneficial. If anyone has had any success achieving something like this please let me know. Thanks!

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

    (@msaari)

    There are two approaches to this:

    1. Do one search for all the posts, then display the results in two tabs split by post type: loop through the posts and display all the pages, then loop again and display the posts. This doesn’t necessarily require any changes in Relevanssi and can all be done in your template, though you can have Relevanssi sort the results by post type to simplify things.
    2. Do two searches: one for pages, one for posts. Since WP is already doing one search, you would have the main query fetch only pages, and then do another query for posts. This is fairly convenient, but of course you’ll be doing two queries instead of one which is less efficient.

    Unfortunately, where both of these solutions get tricky is the paging. It’s hard to get working well, no matter your choice. The first method is easier, as you can just use the normal paging, especially if you don’t sort by post type. The problem is the random distribution of post types among the pages: one page may have 50-50 posts and pages, and another may have 20-80.

    With two searches, it’s hard to track the number of pages in the secondary post type.

    All in all, this is a fairly difficult problem to solve, which is part of why Relevanssi doesn’t have a built-in solution for this (another part is that this involves templates, and Relevanssi doesn’t touch templates).

Viewing 1 replies (of 1 total)
  • The topic ‘Separating search results by post type’ is closed to new replies.