Custom sort order for facet items

  • orbiteers
    Participant
    3 years, 1 month ago #24933

    Hi there,

    Our project requires some widget filters to display their facet items in a custom order (outside the alphabetical and count-based ordering provided). Looking through the code I see the filters are displayed using the facets.twig file, which can be overridden at the theme level using the provided WPSOLR_Events::WPSOLR_ACTION_BEFORE_RENDER_TEMPLATE filter, where I would then have the opportunity to enforce the custom facet item order requested.

    My question: Is the facets.twig file best opportunity for enforcing the custom facet item order, or is there a better option elsewhere in the plugin where I can manipulate the template_data before rendering the filters?

    Thanks again for your help!
    Mark

    wpsolr
    Keymaster
    3 years, 1 month ago #24934

    Just be aware that sort would be performed on client-side only. Which means that if you do not display all facet items, the sort will be partially incorrect.

    You can use WPSOLR’s facet data event WPSOLR_Events::WPSOLR_FILTER_UPDATE_FACETS_DATA for that.

    You can find an example in the Yoast add-on file, where permalinks are added to each facet url: /htdocs/wp-content/plugins/wpsolr-pro/wpsolr/pro/extensions/seo/class-wpsolr-option-seo.php.

    /**
     * Modify the facets data at your convenience just before calling the Twig template for display
     * @param array $facets_data
     * @return array
     */
    public function update_facets_data_skeleton( $facets_data ) {
       // Do something here on facets data
       $facets_data[...] = ...;
       return $facets_data;
    }
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.