facetwp_facet_sources
Overview
Modify the choices within the Data source
dropdown of facets and Listing Builder items, in the FacetWP settings.
This filter is useful for adding custom data sources, or for modifying the existing ones.
If you’re adding new data sources, then you’ll likely also need to use the facetwp_index_row hook to control how the data gets saved to the index table.
Parameters
- $sources | array | An associative array of data sources
- $context | string | (optional) “default” or “builder”
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
$sources = [ 'posts' => [ 'label' => 'Posts', 'choices' => [ 'post_type' => 'Post Type', 'post_date' => 'Post Date', 'post_modified' => 'Post Modified', 'post_title' => 'Post Title', 'post_author' => 'Post Author' ] ], 'taxonomies' => [ 'label' => 'Taxonomies', 'choices' => [ 'tax/category' => 'Categories', 'tax/post_tag' => 'Tags' ] ], 'custom_fields' => [ 'label' => 'Custom Fields', 'choices' => [ 'cf/horsepower' => 'horsepower', 'cf/mpg_city' => 'mpg_city', 'cf/mpg_highway' => 'mpg_highway', 'cf/torque' => 'torque' ] ] ];
Note: each array key (posts, taxonomies, custom_fields) is its own <optgroup>
. By adding new array keys, you can create your own optgroups.
Usage
Add a new custom field named “my_data”:
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_facet_sources', function( $sources ) { $sources['custom_fields']['choices']['cf/my_data'] = 'my_data'; return $sources; });
Add “Post Status” as a Data sources option:
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_facet_sources', function( $sources ) { $sources['posts']['choices']['post_status'] = 'Post Status'; return $sources; });