Modifying Search Results Page

  • duck
    Participant
    2 years ago #29405

    We are attempting to use the cross-domain search function and would like to control how the search results page displays, however, the documentation says we must use “Yes, as the global search (only with ajax)” in the drop down, which appears to be a deprecated feature? According to this post here:
    https://www.wpsolr.com/forums/topic/customize-the-wpsolr-ajax-search-template-shortcode/

    In addition to that, when deselecting a “Type Filter” on the ajax search results page we get this error:
    “Model type is unknown.”

    Separate issue from the cross-domain feature: We also were using the All In One SEO plugin and when enabling that extension in WP Solr it broke the Ajax search results page with our Kadence theme.

    wpsolr
    Keymaster
    2 years ago #29406

    I’ll answer to your first question in this topic. Please create a separate topic for each other question.

    You can use the filter WPSOLR_Events::WPSOLR_ACTION_BEFORE_RENDER_TEMPLATE to redirect to a Twig search template of your own.

    The current Ajax search template file is /wpsolr-templates/twig/search/search.twig if you need to have a look.

    A full example (for suggestions templates, but principles remain the same) can be found in /wpsolr-examples/wpsolr-example-suggestions-custom-templates-twentysixteen-child/functions.php

    • This reply was modified 2 years ago by wpsolr.
    duck
    Participant
    2 years ago #29487

    1. How do I display the last modified date in the twig template?
    For example the published date {{ result.meta_information.date }}

    2. How do I list the subsites as a facet?

    3. Is there a list of twig template options to call?

    If I need to break these out into other forum topics just let me know, thanks,
    Don

    wpsolr
    Keymaster
    2 years ago #29488

    In /wpsolr-examples/wpsolr-example-suggestions-custom-templates-twentysixteen-child/functions.php, you can also find examples on how to add your own data to the twig object results, before rendering with your custom twig template.

    foreach ( $template_params['template_data']['results'] as &$result ) {
      // Retrieve post id from data
      $post_id = $result['PID']; // or $result['id']
      // Store new data
      $result['my_data']['something'] = calculate_something_from_post_id( $post_id );
    }

    Then, in twig:
    {{ result.my_data.something }}

    • This reply was modified 2 years ago by wpsolr.
    duck
    Participant
    2 years ago #29490

    What you are telling me is I need to learn Twig first instead of stumbling my way through reformatting the search results twig templates? Sorry, new to Twig and I was hoping there was an already easy to access reference to elements that WP Solr is using so I can just reuse them instead of creating my own data, I just want to use the existing data.

    Is this how I will need to display the subsites as facets too? Lost on how to show the subsite results as facets right now and this doesn’t seem straight forward without educating myself to be a twig developer or understand better how WP Solr implements cross domain search without more custom programming on my end.

    My apologies if I am coming across as difficult, just not experienced enough to know what to do in regards to Twig templates for the deprecated ajax search templates. Which also concerns me that I am putting forth effort for something that is deprecated and will go away soon?

    duck
    Participant
    2 years ago #29495

    Trying to be more specific. Is it possible to add each subsite as a facet through the twig template, instead of relying on the subsite categories for the facets?

    Since this is in a multisite network I want to remove the ability for our site admins to add endless categories to their sites causing the cross domain search results facets to be overloaded with categories that they add to their sites. I want to add a top level faceted search for just the subsites, and not show every single sub category in the facets. Looking for some guidance on how to achieve this and we may need your assistance to do so.

    wpsolr
    Keymaster
    2 years ago #29503

    Is it possible to add each subsite as a facet

    Add the site name as a custom field or taxonomy term to each post type indexed in screen 2.2, then select the custom field in screen 2.2 and 2.4. Reindex. That’s it. No need to modify the Twig templates.

    duck
    Participant
    2 years ago #29509

    Okay excellent, that helps point me in the right direction and glad I don’t need to become a twig expert, just yet! 🙂 I will just need to make sure our content editors do not have access to remove the custom taxonomy from the page, which should be feasible.

    One last question, is there not an easy way to add the modified date to the search results in twig like the publish date: {{ result.meta_information.date }}

    If the answer is no, there is not already an object I can call, then I will table this for now until I have more time to become a twig expert in order to pull it from the post. I was just hoping that since we can sort by modified that the object already exists. Thanks for your help on this!

    wpsolr
    Keymaster
    2 years ago #29513

    is there not an easy way to add the modified date to the search results in twig like the publish date: {{ result.meta_information.date }}

    Indeed, {{ result.displaymodified }} should work

    You can display all your Twig data content by uncommenting the dump command located on each Twig template header file.

    For result.twig, copy {{ dump(result) }} to the first line after the header.

    • This reply was modified 2 years ago by wpsolr.
    wpsolr
    Keymaster
    2 years ago #29515

    You can also format your dates with Twig date filters: https://twig.symfony.com/doc/3.x/filters/date.html

    duck
    Participant
    2 years ago #29517

    Thank you thank you, the {{ dump(result) }} is exactly what I was looking for so I could list out all of the objects that are available to use!

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.