How to check if search results contain a specific object type?

Topic summary

A developer needed to detect which object types (products, articles, or both) appear in Shopify search results to apply different styling and layouts accordingly.

Problem:

  • Wanted conditional logic to check if search results contain specific object types
  • Tried various approaches using Shopify’s search object and predictive_search object without success
  • Needed different page layouts depending on whether results included products only, articles only, or both

Solution provided:
Loop through search results items and set boolean flags:

assign has_products = false
assign has_articles = false

for item in items
  if item.object_type == 'product'
    assign has_products = true
  elsif item.object_type == 'article'
    assign has_articles = true
  endif
endfor

These flags can then be used in conditional statements to render different sections or apply different styles based on result composition.

Outcome:
The solution successfully resolved the issue, allowing the developer to implement the desired conditional styling and layout changes.

Summarized with AI on November 13. AI used: claude-sonnet-4-5-20250929.

I am trying to find a way to ask the question with some kind of conditional statement, if the search results on the search results page contain specific types (e.g. product, article). If I can get that information, then I can style the search results page accordingly, based on whether articles + products appear, or just articles, or just products. The layout needs to be quite different depending on what the results are, which types appear, and that’s my goal with the question here.

Below are the code examples from my files, that contain the most important information.

main-search.liquid file:

{%- render 'predictive-search', context: 'search-page' -%}

    {%- if search.performed -%}
      
        {%- if search.results_count == 0 -%}
          

            {{ 'general.search.no_results_html' | t: terms: search.terms | replace: '*', '' }}
          

        {%- endif -%}
      

      {%- if search.results_count != 0 -%}
        {%- assign paginate_by = per_row | times: page_per_row -%}
        {%- paginate search.results by paginate_by -%}
          
            

              {% render 'search-collection-grid',
                collection: search,
                items: search.results,
                enable_sidebar: section.settings.enable_sidebar,
                filter_style: section.settings.filter_style,
                enable_sort: false,
                enable_collection_count: true
              %}

              {%- if paginate.pages > 1 -%}
                {%- render 'pagination', paginate: paginate -%}
              {%- endif -%}
            

          

        {%- endpaginate -%}
      {% endif %}
    {%- endif -%}

search-collection-grid.liquid file:

{% liquid
  if collection.products
    assign count = collection.products_count
    assign count_label = 'collections.general.items_with_count'
  endif

  if collection.results
    assign count = collection.results_count
    assign count_label = 'general.search.result_count'
  endif
%}

  
    

      {%- for item in items -%}
        {%- if item.object_type == 'product' -%}
          {% if forloop.index == 1 %}
            ### Products
          {% endif %}
          {%- render 'search-product-grid-item', product: item -%}
        {%- endif -%}
      {%- endfor -%}
    

    
      ### Recipes

      {%- for item in items -%}
        {%- if item.object_type != 'product' -%}
          {%- render 'search-recipe-grid-item', item: item %}
        {%- endif -%}
      {%- endfor -%}
    

  

search-recipe-grid-item.liquid file:


  

    
      
        {%- if item.object_type == 'article' and item.image -%}
          {%-
            render 'image-element',
            img: item.image,
            classes: 'grid-product__image',
            alt: item.title,
            widths: '180, 360, 540',
            sizeVariable: sizeVariable,
            fallback: fallback,
          -%}
        {%- endif -%}
      
      {{ item.title }}
    
  

I tried to wrap all kinds of different conditionals around for {% item in items %} forloop, to check if any products came through the search, but nothing in the Shopify docs or any of the search object or predictive_search object conditionals and options worked to ask that question. It seems like it should be really simple to ask, on the search results page, what object type has come through the search? Then to apply styles etc accordingly. Why should product or article headings or styling appear when that object type isn’t there? Makes no sense. Anyway, happy to receive any guidance on this.

Try this:

assign has_products = false
  assign has_articles = false

  for item in items
    if item.object_type == 'product'
      assign has_products = true
    elsif item.object_type == 'article'
      assign has_articles = true
    endif
  endfor

Then you can use these flags to conditionally render different sections or apply different styles:

{% if has_products and has_articles %}
  
{% elsif has_products %}
  
{% elsif has_articles %}
  
{% endif %}

Wait, I totally messed that up. Let me actually try your code on my dev site.

1 Like

Thanks so much! Sorry for not replying sooner. This totally did the trick. Was exactly what I needed, and solved several problems. Once again, thanks so much @SomeUsernameHe