Separate Products & Blog Posts in Search Results

Is there a way to separate products and blog posts in search results?

As it stands right now, they’re all intermingled. I’d like for products to show first, and then blog posts. Or add a filter to filter in/out blog posts (with filter “out” as the default)

store URL is turbojavacoffee.com

@ebo8302
You can try this code to achieve your result

{% paginate search.results by section.settings.search_result_show_desktop %}
          {% assign productCount = 0 %}
          {% assign articleCount = 0 %}
          {% assign pageCount = 0 %}
          {% for item in search.results %}
            {% case item.object_type %}
              {% when 'page' %}
                {% assign pageCount = pageCount | plus: 1 %}
              {% when 'article' %}
                {% assign articleCount = articleCount | plus: 1 %}
              {% when 'product' %}
                {% assign productCount = productCount | plus: 1 %}
            {% endcase %}
          {% endfor %}
          {% if productCount != 0 %}
            

              {{ 'main-search.product' | t }} ({{ productCount }})
            

          {% endif %}
          
            {% liquid
              assign index = 1
            %}
            {% for item in search.results %}
              {% if item.object_type == 'product' %}
                {% assign id = 'main-search-' | append: forloop.index %}
                

                  {%
                    render 'card-product',
                    product: item,
                    show_tags: {{section.settings.show_tags}},
                    show_rating: {{section.settings.show_rating}},
                    show_as_card: {{section.settings.show_as_card}},
                    show_product_status: true,
                    button_style: {{section.settings.button_style}},
                    index: index,
                    id: id
                  %}

                  {% liquid
                    assign index = index | plus: 1
                    if index > section.settings.columns_desktop
                      assign index = 1
                    endif
                  %}
                

              {% endif %}
            {% endfor %}
          

          {% if articleCount != 0 %}
            

              {{ 'main-search.article' | t }}({{ articleCount }})
            

          {% endif %}
          
            {% liquid
              assign index = 1
            %}

            {% for item in search.results %}
              {% if item.object_type == 'article' %}
                

                  {%
                    render 'article-card',
                    article: item,
                    show_featured_image: true,
                    show_date: {{section.settings.article_show_date}},
                    show_author: {{section.settings.article_show_author}},
                    show_excerpt: false,
                    index: index
                  %}

                  {% liquid
                    assign index = index | plus: 1
                    if index > section.settings.columns_desktop
                      assign index = 1
                    endif
                  %}
                

              {% endif %}
            {% endfor %}
          

          {% if pageCount != 0 %}
            

              {{ 'main-search.page' | t }}({{ pageCount }})
            

            
              {% liquid
                assign index = 1
              %}

              {% for item in search.results %}
                {% if item.object_type == 'page' %}
                  

                    {{ item.title }}
                  

                  {% liquid
                    assign index = index | plus: 1
                    if index > section.settings.columns_desktop
                      assign index = 1
                    endif
                  %}
                {% endif %}
              {% endfor %}
            

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

        {%- endpaginate -%}

Like and Reply to reach you further

Joy Matubber| Brain Station 23

  • Was your question answered? Mark it as an Accepted Solution
  • Was my reply helpful? Click Like to let me know!