Skip Navigation

[Resolved] Views custom filtering by post published date year on front-end

This support ticket is created 4 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by JeremyM3920 4 years ago.

Assisted by: Waqar.

Author
Posts
#1564749

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!

#1565153

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

date-filter-image.png

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

#1566155

Amazing, thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.