wpsl_get_terms_args

  1. Description
  2. Parameters
  3. Usage
  4. Changelog
  5. Source File
  6. Related

Description

This filter allows you to change the arguments that are passed to get_terms() to load the store categories for the category filter.

Which category filters it affects depends on the filter type and the template:

  • Checkboxes in the Default and Horizontal templates. This is when “Filter type” on Store Locator > Settings > Search (Search settings) is set to “Checkboxes”, or the shortcode has [wpsl category_filter_type="checkboxes"].

    The checkboxes are built from the categories this query returns, so the filter sets which categories are shown and in which order. Child categories are shown in the same list as their parents.

  • The dropdown in the Default and Horizontal templates. The dropdown itself is built with wp_dropdown_categories() and the arguments from wpsl_dropdown_category_args. This filter only decides if the dropdown is shown at all. If this query returns no categories, the dropdown is left out.
  • The Vertical template. The category list in the panel is loaded with its own get_terms() call, so this filter doesn’t change the categories in it or their order. Here too, the filter only decides if the category filter is shown at all.

These are the default values that are used:

Key Default
taxonomy 'wpsl_store_category'
exclude The category IDs from the exclude_category shortcode attribute, otherwise ''.
parent The category_parent_id shortcode attribute when it’s a top-level category, otherwise ''.
hide_if_empty true. This isn’t a get_terms() argument, so it has no effect.

The other arguments use the get_terms() defaults. The categories are sorted by name (orderby 'name', order 'ASC'), and categories without locations are hidden, because hide_empty defaults to true.

Parameters

$args - (array) The get_terms() arguments.

Usage

This example also shows the categories that don’t have any locations yet, by setting hide_empty to false. It hooks into wpsl_dropdown_category_args as well, because the dropdown loads its own list of categories and would still hide the empty ones. Visitors who select an empty category don’t get any results.

add_filter( 'wpsl_get_terms_args', 'custom_category_terms_args' );
add_filter( 'wpsl_dropdown_category_args', 'custom_category_terms_args' );
function custom_category_terms_args( $args ) {
// Also show the categories that don't have any locations yet.
$args['hide_empty'] = false;
return $args;
}

The Vertical template already shows the categories without locations, so it doesn’t need this code.

This example sorts the categories by ID, so the oldest category shows first and the newest one last. It hooks into both filters, so the checkboxes and the dropdown use the same order:

add_filter( 'wpsl_dropdown_category_args', 'custom_category_order' );
add_filter( 'wpsl_get_terms_args', 'custom_category_order' );
function custom_category_order( $args ) {
// Show the oldest category first, and the newest one last.
$args['orderby'] = 'term_id';
$args['order'] = 'ASC';
return $args;
}

If you use a plugin that lets you drag the categories into your own order, like Category Order and Taxonomy Terms Order, use 'term_order' instead of 'term_id'. Without such a plugin, WordPress sorts on 'term_order' in the same way as on 'term_id'.

The Vertical template ignores this filter for its category list. But with the “Auto Sort” option of the Category Order and Taxonomy Terms Order plugin turned on, which is the default, that plugin sorts every category list in your own order, including the one in the Vertical template, and the orderby argument is ignored. You don’t need any code for that. See Change the Category Order.

Don’t change the taxonomy argument, and make sure the function always returns the $args array.

Changelog

VersionDescription
3.0Introduced.

Source File

This filter is located in /includes/core/templates/class-filters.php.

Placement

The code needs to be placed in the functions.php file inside your active theme folder.