3

I'm using the Expanded Search plugin to display results of the search query with an excerpt of the text matching the query for each result. I want to limit the results to 10 per page, using pagination.

Does anyone know how I could limit the expandedResults from the Expanded Search plugin?

The pagination works with the following code, but the Expanded Search results does not respect the limit to entries and instead shows all the results without limit, on each paginated page (e.g. If I have 14 results, the pagination will have 2 pages available, but all 14 results are displayed across both pages):

Expanded Search Results

  {# Get the user's search query from the 'q' query-string param #}
  {% set searchQuery = craft.app.request.getParam('q') %}

  {# Fetch entries that match the search query #}
  {% set entries = craft.entries()
      .search(searchQuery)
      .orderBy('score') 
      .limit(10) %}

  {# Paginate the entries #}
  {% paginate entries as pageInfo, pageEntries %}

  {# Get the search result excerpts from the Expanded Results plugin #}
  {% set expandedResults = craft.expandedSearch.search(searchQuery, { length: 155 }) %}

  {% if entries|length %}

      {# List search result entries #}
      {% for result in expandedResults %}
          <a href="{{ result.entry.url }}">
              <h3>{{ result.entry.title }}</h3>
              <cite>{{ result.entry.url }}</cite>
          </a>
          {% if result.matchedValue %}<p>{{ result.matchedValue | raw }}</p>{% else %}<p>No preview available</p>{% endif %}
          <p>{% if result.entry.dateUpdated %}{{ result.entry.dateUpdated | date("j F Y") }}{% else %}{{ result.entry.postDate | date("j F Y") }}{% endif %}</p>
      {% endfor %}

      {# Display pagination if there are more than 10 entries #}
      {% if pageInfo.prevUrl or pageInfo.nextUrl %}
            {% if pageInfo.prevUrl %}<a href="{{ pageInfo.prevUrl }}?q={{ searchQuery }}">Previous Page</a>{% endif %}
            {% for page, url in pageInfo.getPrevUrls(2) %}
                <a href="{{ url }}?q={{ searchQuery }}">{{ page }}</a>
            {% endfor %}
            {{ pageInfo.currentPage }}
            {% for page, url in pageInfo.getNextUrls(2) %}
                <a href="{{ url }}?q={{ searchQuery }}">{{ page }}</a>
            {% endfor %}
            {% if pageInfo.nextUrl %}<a href="{{ pageInfo.nextUrl }}?q={{ searchQuery }}">Next Page</a>{% endif %}
            <p>Page {{ pageInfo.currentPage }} of {{ pageInfo.totalPages }}</p>
      {% endif %}

  {% endif %}

I've also tried it this way, where I'm using the expandedResults for the result excerpt only. As expected, the pagination and entry limit is this time respected, but this time, every result excerpt displays the excerpts from all the results (e.g. I have 14 results, split between 2 paginated pages but each result features all 14 excerpts):

Entry Results with Partial Expanded Search Result

{# List search result entries #}
{% for entry in pageEntries %}
    <a href="{{ entry.url }}">
        <h3>{{ entry.title }}</h3>
        <cite>{{ entry.url }}</cite>
    </a>
    {# Show excerpt with matching value if available #}
    {% for result in expandedResults %}
        {% if result.matchedValue %}<p>{{ result.matchedValue | raw }}</p>{% else %}<p>No preview available</p>{% endif %}
    {% endfor %}
    <p>{% if entry.dateUpdated %}{{ entry.dateUpdated | date("j F Y") }}{% else %}{{ entry.postDate | date("j F Y") }}{% endif %}</p>
{% endfor %}
2

This was solved in my latest release: https://github.com/mustasj/expanded-search/releases

So if you want to use it with pagination, you can now add limit and offset to the settings param.

So in your case the top of your template would look something like this:

{% set limit = 10 %}
{% set entries = craft.entries()
    .search(searchQuery)
    .orderBy('score')
    .limit(limit) %}

{# Paginate the entries #}
{% paginate entries as pageInfo, pageEntries %}
{% set expandedResults = craft.expandedSearch.search(searchQuery, {
    length: 155,
    offset: (pageInfo.currentPage - 1) * limit,
    limit: limit
}) %}
  • Thanks for adding that in! – Phil Gwynne Apr 1 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.