facetwp_excluded_custom_fields_like
Overview
In FacetWP’s admin area, FacetWP uses the FWP()->helper->get_data_sources() function to fetch existing custom fields, to show them in each facet’s “Data source” setting dropdown and in the dropdown used to add new Listing Builder listing items.
In some cases, you may want to prevent one or more custom field(s) from showing up in these dropdowns. This hook (available in FacetWP v4.5+) can be used to exclude specified fields with a partial match, e.g. for a range of fields that have a hashed part of their name. This hook uses the FWP()->helper->get_data_sources() function’s NOT LIKE SQL clause for its partial matching.
A use case would be to remove a range of fields that heavily pollute the dropdowns. For example, we’ve seen issues with certain plugins that continually add postmeta rows, which then blow up the amount of fields to fetch, causing 404 (or 403 or 500) errors in the FacetWP admin area.
Note that there is also a sister hook, facetwp_excluded_custom_fields, with which you can filter out fields with an exact match of the field name.
For more elaborate customizations, you could also use the facetwp_facet_sources hook. This hook can be used to customize the entire list of data sources, including taxonomies and fields from other plugins that FacetWP integrates with, and their labels. It also lets you add new fields and field groups, or change the order of the groups in the dropdown. The results of this hook are not cached though, so it is recommended to use the facetwp_excluded_custom_fields hook or facetwp_excluded_custom_fields_like hook, if possible.
Parameters
- $not_like | array | An array of strings to use as a partial match to exclude fields by name
Example
The following example removes custom fields that have _crp_cache_ in their name from the facets’ and Listing Builder listing items’ Data source dropdowns:
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
add_filter( 'facetwp_excluded_custom_fields_like', function( $not_like ) { $not_like[] = '_crp_cache_'; // Exclude custom fields that have '_crp_cache_' in their name from all data sources dropdowns return $not_like; });