facetwp_facet_sources

Overview

Modify the choices within the Data Sources (facet) box.

This filter is useful for adding custom data sources, or for modifying the existing data sources.

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

$sources = array(
    'posts' => array(
        'label' => 'Posts',
        'choices' => array(
            'post_type' => 'Post Type',
            'post_date' => 'Post Date',
            'post_modified' => 'Post Modified',
            'post_title' => 'Post Title',
            'post_author' => 'Post Author'
        )
    ),
    'taxonomies' => array(
        'label' => 'Taxonomies',
        'choices' => array(
            'tax/category' => 'Categories',
            'tax/post_tag' => 'Tags'
        )
    ),
    'custom_fields' => array(
        'label' => 'Custom Fields',
        'choices' => array(
            '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”:

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:

add_filter( 'facetwp_facet_sources', function( $sources ) {
    $sources['posts']['choices']['post_status'] = 'Post Status';
    return $sources;
});

More examples