Loop search results with 3.0

Previously I used this to loop search results:

<Loop search="{Url query=s}" >

That’s not working in 3.0 and I don’t see search anywhere in the new docs for loop

Is there a new way to loop search results?

Cheers, Richard

Hey Richard, I love your new user flair haha! :beetle: :mag:

As far as finding this in the docs, I’m pretty sure it can be found in the same place as it was previously, which is in the list of available query parameters for post loops. I believe what your template is doing isn’t so much “looping through search results” as it is using the default loop of the search page and adding a search query parameter that filters results to only show items that match the search terms located in the URL query parameter. The reason you’re not seeing search in the Loop tag section of the docs is that it’s a query parameter for a specific type of loop, not its own loop type.

I know that Eliot changed the way L&L gets its default loop context in a few places in Gutenberg to fix a couple of instances where L&L wasn’t able to loop through the current post, so maybe that impacted how it’s getting its loop context on your search results page. We’ll look into that.

Out of curiosity, if you add type=post to your loop, does that fix/change anything?

Edit: I was able to replicate this and it does indeed seem like a bug. I’ll add this to the to-do list.

1 Like

Maybe I really am an Expert Bug Finder. :grinning:

I’m searching six custom post types, plus the default post type.

Adding type=post shows results from the default post type, but not any of the CPTs.

Cheers, Richard

2 Likes

The type parameter can actually accept an array so if you want a workaround, I’m assuming you could add type=post,quotes,another_post_type,etc to loop through all your post types. It should be functionally the same thing. But I’m sure Eliot will be able to address this quickly so we’ll report back here when there’s an update about this.

1 Like

FYI, adding the other post types does show them on the results page, but FacetWP no longer works to filter the results and my snippet no longer works to sort the results.

Ah, right that makes sense. I didn’t realize you were using FacetWP to filter the search results, which requires using the default loop without any post type specified, so my workaround wouldn’t work for that.

Any idea of the timetable for an update?

@eliot’s communications with me have largely come in the form of LOTR references so I think this would be the best explanation of when we can expect the update. I’ll keep this thread updated about this issue.

2 Likes

I’m looking into the issue, and so far, on my end, the search result page is working with the default loop context using <Loop>.

Here’s the layout template I tried:

<h4>Search results</h4>

<If loop exists>
  <Loop>
    - <Field title /><br>
  </Loop>
<Else />
  Nothing found.
</If>

In the Location tab, I selected it to override the Search Result page. Matching results are showing for all post types.

So I believe this issue (and the other one about search results) are specific to FacetWP.

Could someone give me access to a test site (with FacetWP installed) that I can copy locally to study the issue in depth?

1 Like

I disabled FacetWP. I am using SearchWP

I’m use a Beaver Themer Template for my search results. I added a Tangible Template, plugged in your exact code, i.e.

<If loop exists>
  <Loop>
etc.

and got Nothing found. I tried this:

<Loop search="{Url query=s}" >
- <Field title /><br>

and got nothing.

I tried this:

<Loop type=facts,people,post,product,quotes,goods,memes search="{Url query=s}" >
- <Field title /><br>

Now I see the results, whether FacetWP is disabled or not.

Perhaps if we figured out why your original code isn’t working, the issue of FacetWP would become moot.

@eliot Could you please tell me where to find the Location tab you referenced?

When you create a layout (as opposed to a template) you can select a location where you want the L&L markup to override a part of the theme, which is what Eliot was referring to.

1 Like

Thanks @benjamin I was putting the code written by @eliot into a Beaver Themer Layout, which didn’t work. When I create a Tangible Layout it does work. Is that expected behavior or should the code be expected to work in either location?

If it will only work in a Tangible Layout, I would need to import all the rest of my Beaver Themer page layout into the Tangible Layout.

Ah I understand, so this is specifically an issue with a Beaver Themer template that’s overwriting a search results page. I’m now able to replicate this issue so this should be easier to look into now. I’ll follow up here with updates.

1 Like

Hey Richard, I haven’t tested this myself but I just noticed that version 3.1.1 of the plugin (which was released on December 30) Included the fix “Loop: Improve getting default loop context for search results archive” so it seems that this issue may be resolved now. Have you noticed an improvement since that release?

Dear @benjamin, How do I possible to work a look context for search archive? Could you help me? Please!
I’m working with Divi Builder, it shows very well Completed Loop of all one CPT, but when I intent to Search by any keyword argument in “Search Module” (native of Divi) it passes the search argument to “Divi Builder Search Template” very well.
The URL is correct “…/?s=alonso&et_pb_searchform_submit=et_search_proccess”

The “Divi Builder Search Template” contains the shortcode of another template to search of L&L:

<Loop type=clientes paged=3 earch="{Url query=keywords}" >
<div class="ov-card">
    <div class="titulo">
      <h3><Field title /></h3>
    </div>
 
    <p class="campos-datos">Nº Cliente: <b><Field acf_editor=n_cliente_clientes /></b></p>
    <p class="campos-datos">N.I.F. / D.N.I.: <b><Field acf_editor=nif_dni_clientes /></b></p>
    
 // and more acf-fields...
      
</div>
</Loop>

The first sentence indicates that the Look only creates each card of the loop if this keyword is in URL, and you can see that it is after/?s=, “alonso” is in this case. And no! … :disappointed: I’m so sad because the Look L&L show me all CPT published, no only the results of query:

(it didn’t permited show you and image capture). For private I can tell you the Url Site to test yourself.)

I am not using plugin to search, Divi Search is very clean. And I to be very sure the search into CPT are in correct CPT, this lines of code in function.php are to this:

function buscar_cpt( $query ) {
 if ( $query->is_search ) { $query->set( 'post_type', array( 'clientes', 'autos' )); } 
 return $query;
}
add_filter( 'the_search_query', 'buscar_cpt' );

With additional snipet, without it, with and without plugin… I couldn’t do so important Search, for any APP online is necessary this feature, not only for me.

Could you help me? Please! and Thank you very much!!!

Your query “keywords” won’t do anything, and your snippet also uses “earch” instead of “search”. Could you try this?

<Loop type=clientes paged=3 search="{Url query=s}">
  <div class="ov-card">
      <div class="titulo">
        <h3><Field title /></h3>
      </div>
   
      <p class="campos-datos">Nº Cliente: <b><Field acf_editor=n_cliente_clientes /></b></p>
      <p class="campos-datos">N.I.F. / D.N.I.: <b><Field acf_editor=nif_dni_clientes /></b></p>
      
   // and more acf-fields...
  </div>
</Loop>

Otherwise, it’s worth trying a default Loop without any parameters:

<Loop>...</Loop>

Dear @julia! I am extremely grateful with you and your L&L, I just got the solution today, the problem wasn’t with L&L (your first replay goes very well, now :heart_eyes:). Divi’s Support’ve given me a PHP code to enable CPT in Divi native Search Module, without plugins to look for.
Wonderfull!!! I just know your L&L and I love it!!! . Thank you very much!

3 Likes