How to loop Search Results

The Beaver Builder search results template creates a main loop with multiple post types.

How could I loop through the search results and display each post type differently, i.e.

<If type="fact" >
    <Template name=fact-core />
</If>
<If type="person" >
    <Template name=person-core />
</If>

I’m thinking:
1: use a posts module to send the each post id to a template that can figure out what kind of post it is based on the id
or maybe
2: Somehow create an L&L loop based on search results

Cheers, Richard

I haven’t played around very much with that condition, but it seems to be designed to allow for conditional logic depending on the current type of page you’re on. So <If singular type=page> would be true if it was placed on a singular page but would be false if it was placed on a singular post or an archive page. It seems to be the counterpart to the archive condition, so <If archive type=person> would be true if you placed it on the archive page of your person CPT but would be false if placed on a singular person post. I’m not sure exactly how the Beaver Builder search results template works or exactly what you’re trying to achieve, but if your template is running once per post in the search results, then something like <If singular type="fact" > might allow you to achieve what you’re trying to do.

Thanks, @benjamin

I’ve reworded the question because I think I need to approach the whole problem differently.

I’d probably go with option 1, since option 2 sounds like it might involve messing with the query in PHP and I can’t help much with that. You mentioned that you’d “send each post id to a template” and I know you had had issues with the BB posts module and L&L in the past, which I’ve been able to replicate. While we’re on the topic, I thought I’d mention that version 3.0.0 of the plugin will be getting released imminently and includes a fix for that behaviour, so you should now just be able to add templates to the layout of your BB posts modules and have it work as expected without needing to pass the post ID as a variable to the template.

I think I’d probably use Switch and When for this just to keep the code a little more streamlined instead of a bunch of distinct If statements.

In any case, I don’t think this requires a change in approach from how you had originally suggested you were going to approach this. Until 3.0.0 drops you can pass the post ID as a variable to the template but once you get the update you can simply embed the template normally with the shortcode and it’ll get the current context from each post as intended. Then just create some logic (either with If as I wrote in my earlier comment or with Switch and When if you want to make things a bit more compact) and you should be good to go. Let me know if you get stuck or run into issues implementing this.

Hi Richard, could you try formatting your search archive loop like this: <Loop search="{Url query=s}" orderby=relevance order=desc>? Then you can use <Switch field=type><When value="post_type_slug" /></Switch> to change the layout within each item. Let me know if this works for you :slight_smile:

3 Likes

First, thanks for engaging and I’m excited about 3.0.0. Feels like Christmas is coming.

Second, I put Julia’s answer straight into my Search Results Template (as html) and it works! Very Impressive. I only have some niggles with FacetWP.

Third, I know documentation is down the list of priorities, but I have found the <If > tags to be particularly confusing.

The doc says: The basic structure is: <If subject comparison value>

Then it lists Core conditions, some of which include an equals sign, others don’t. Are those the subject? Does the equals sign replace the comparison?

I’m not needing answers to this now, but maybe someone would like to know what’s tripping me up.

Also, whenever documentation is addressed, I hope you create opportunities for user-supplied examples. That’s the most useful documentation for me, and I’d be happy to supply examples of code that has been useful to me. (Every so often I get notified that someone was able to use something I’ve posted. Today, I got thanked for a post I made in 2009.)

Finally, FWIW I had glossed over Switch and When because the terminology was foreign to me, but it’s really just another label for a “case” statement.

Cheers,

1 Like

FYI, Using Loops with FacetWP - Tangible Inc. Knowledge Base

In summary, to use L&L for search results with multiple post types with FacetWP, put this on the search results page:

<div class="facetwp-template">
 <Loop search="{Url query=s}" >
  <Switch field=type>

    <When value="dogs" />
     <div class="{Field post_class}">
       <Template name=dogs />            <!-- whatever you want here -->
     </div>

    <When value="cats" />
     <div class="{Field post_class}">
       <Template name=cats />            <!-- whatever you want here -->
     </div>

<!-- etc. -->

  </Switch>
 </Loop>
</div>

I also have php snippets to group results by post type (ordered within groups by relevance or whatever) and to change the order of checkboxes in Facet. I’ll be happy to post those if anyone wants them.

Cheers, Richard

I’m still putting the finishing touches on the new docs, but I’m hoping to launch them alongside 3.0.0. You can see that it’ll have more examples and explanations throughout and I’m working to pull more examples to add from these forums. As you were noting, I expanded a lot more on the If tag. Feel free to give that “overview” section a read and let me know if that clears up your confusion about what a subject is vs what a core comparison is. I’ve also added examples for each of the core conditions so hopefully that clears up the syntax for those.

1 Like

Excellent. I feel sorry for anyone who is using WordPress and not using L&L.

2 Likes