Content Filters

It is possible to define custom filters for each post type by implementing simple WordPress filter function. All filters located in Workforce are defined by just one simple array. This array contains arrays which belongs to post type so the array key is just custom post type identifier.

When the plugin is rendering an archive page for custom post type it always check for the result of workforce_filters. If the result is not empty and array key exists and it is same as current archive page post type, it starts rendering filter fields.

function mytheme_custom_filter( $filters ) {
    $filters['invoice'] = [ // Array key is post type identifier.
        [
            'input_type'    => 'text',
            'value'         => ! empty( $_GET['custom_field_id'] ) ? $_GET['custom_field_id'] : null,
            'placeholder'   => esc_attr__( 'Search for string', 'mytheme' ),
            'label'         => esc_html__( 'Keyword', 'mytheme' ),
            'key'           => 'custom_field_id',
            'compare'       => '>=',
            'type'          => 'NUMERIC',
        ],
    ];

    return $filters;
}

add_filter( 'workforce_filters', 'mytheme_custom_filter' );

Workforce filtering is based on pre_get_posts action. Where it is checked if the filter variables are available. If the filter variables are found plugin starts building WordPress query. You can overwrite built queries through workforce_filters_meta_query and workforce_filters_tax_query WordPress filters (more information in Filters section).

You can see an implementation of each filter in workforce / src / Type / PostType.php. There you can find an examples with the taxonomy or datepicker fields.

When developing new filters we recommend to not change Workforce plugin code nor parent theme. Use child theme or create new plugin with custom implementation.

results matching ""

    No results matching ""