Adjusting or controlling search results

  • I've been asked to retrospectively add search into a RunWay site. After a fair amount of work (and template adjustments) this is now done. I've also learned some future things affecting the way I build future templates, in case search needs to be added. But I have one issue I cannot resolve ...


    I hold 'vacancies' and 'news' items in collections. Each is set with a 'visible' switch, enabling the author to set up news or a vacancy in advance and then make it visible on pages when they are ready. However, these news/vacancy items still appear in search results whether or not the switch is set.


    Is there a way I can control what is searched and not return a result if the visible switch is not set to 'yes'?


    The template code for each occurrence of this switch is consistent in all templates ...

    <perch:content id="visible" type="checkbox" label="Visible?" value="yes" searchable="false" suppress divider-before="Make this Item visible?" />


    Thanks, Graham

  • One option is to filter the results inside the template with perch:if tags. While this is perhaps the easiest approach, it doesn't work well with pagination (number of results per page is inconsistent) and you cannot easily output a no results message.



    An alternative approach would be to fetch all search results, skip the templating and filter in PHP:


    PHP
    1. $search_items = perch_content_search(perch_get('q'), [
    2. 'skip-template' => true,
    3. 'count' => null,
    4. ]);
    5. // filter $search_items


    Then you can render a template using perch_template() (or pipit_template() if you need pagination).