It is possible to add custom filters to a View that cannot ordinarily be added through the View editor, such as for post fields like the post author or post date.
It requires some coding, which I will describe below.
There are 3 parts to any solution.
The first is to add a filter control to the output of the View, so that users can select the value to filter by. Because this is a custom control it won't be possible to insert it using the normal UI, it will need to be inserted using a shortcode.
The second is a requirement to register the parameter used by this control with Views, as the search parameters require special handling.
The third is to use the Views API to modify the database query to use the value of the filter, if provided.
I will share two examples, for the more popular use cases of filtering by post date, and for filtering by post author.
I recommend adding one code snippet at Toolset > Settings > Custom Code with the relevant code for each custom filter added.
Below I detail adding a post date filter. I will share details of adding a post author filter in a separate reply.
Adding a post date filter
Say we want to add a filter that is a select dropdown which offers several choices, such as posts within the last week, within the last month, within the last year, etc.
The first part involves adding the dropdown to the filter section. In this example, the legacy Views shortcode wpv-control can be used. In the legacy Views editor you can add shortcodes directly, but with the block editor you will need to insert a Shortcode block or Custom HTML block and add the shortcode there.
We will add this shortcode:
[wpv-control url_param="date_filter" type="select" values=",1 day ago,1 week ago,1 month ago,1 year ago" display_values="Anytime,Today,Past week,Past month,Past year"]
Note that it specifies the URL parameter that will be used for this filter ("date_filter"), and that the values for the select options are passed as a list in the "values" attribute, while the corresponding option labels are passed in the "display_values" attribute. (The first list item in the values attribute is empty, corresponding to the "Anytime" label for when no filter value is selected.)
The second part of the solution is to register the URL parameter attributes with Views, as shown here.
It is only necessary to update the ID of the View this filter is being added to, along with the attribute element to specify the URL parameter used by the filter.
add_filter( 'wpv_filter_register_url_parameters_for_posts', function( $attributes, $viewSettings ) {
if ($viewSettings['view_id'] == 123 ) { // Edit ID of View as required
// Register the custom URL parameter author_filter
$attributes[] = [
'attribute' => 'date_filter', // the URL parameter for this filter
// generic values, ignore
'query_type' => 'posts',
'filter_type' => 'custom',
'filter_label' => 'Custom',
'expected' => 'string',
'value' => 'id',
'placeholder' => '',
'description' => '',
];
}
return $attributes;
}, 99, 2 );
The third step is to use the Views API to modify the resulting query whenever the search filter is used and apply its value. In this example we will use 'post_modified' for the comparison rather than 'post_date', i.e. when the post was last edited rather than created.
This means using the wpv_filter_query hook, as shown below.
It is necessary to edit the View ID this should apply to, and also to edit the URL parameter (in this case 'date_filter' which occurs 3 times below).
// 3. modify View query filter to apply filter value
add_filter( 'wpv_filter_query', function($query_args, $view_settings, $view_id){
if ( $view_id == 123 && isset($_GET['date_filter']) && !empty($_GET['date_filter'][0]) ){
$query_args['date_query'] = array(
'after' => $_GET['date_filter'][0],
'column' => 'post_modified'
);
}
return $query_args;
}, 101, 3 );
Be aware if you are modifying this solution to your own needs that when you insert a filter control using the wpv-control shortcode, it treats the URL parameters as arrays (check the $_GET object and you will observe the value of the date_filter parameter will be available in $_GET['date_filter'][0] rather than just $_GET['date_filter'].
This example takes advantage of the flexible interpretation of "dates" in the WordPress date query, see versteckter Link and https://developer.wordpress.org/reference/classes/wp_query/#date-parameters
The three steps above should give you a functioning post date custom filter.