I have a View to display a feed of custom post type News at hidden link and I'd like to allow the user to filter by published date of year. In my Views, I have the following:
[wpv-filter-controls]
[wpml-string context="wpv-views"]Filter News by Year:[/wpml-string] [wpv-control url_param="wpv-year" type="select" values=",2020,2019, 2018" display_values="All,2020,2019,2018"]
[/wpv-filter-controls]
In my functions.php, I have the following:
add_filter( 'wpv_filter_query', 'year_func', 20, 3 );
function year_func( $query_args, $settings, $view_id ) {
if ( $view_id == 4312 && isset($_GET['wpv-year'][0]) && !empty($_GET['wpv-year'][0]) ) {
$star_date = strtotime("01/01/" . $_GET['wpv-year'][0]);
$end_date = strtotime("01/01/" . ($_GET['wpv-year'][0] + 1));
$query_args['meta_query'] = array(
array(
'key' => 'wpv-post-date',
'value' => array($star_date, $end_date),
'compare' => 'BETWEEN',
),
);
}
return $query_args;
}
How can I get this filtering to work as the above doesn't seem to work for me. Would appreciate guidance, thank you!
Hi Jeremy,
Thank you for contacting us and I'd be happy to assist.
In my tests, adding a post date filter directly works to achieve the same.
( ref: https://toolset.com/documentation/user-guides/views/date-filters/ )
You can remove the PHP code snippet for the "wpv_filter_query" filter and include the post date filter in the view's "Query Filter" section, as shown in the attached screenshot.
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar