Skip Navigation

[Resolved] Filtering Posts by Category and Year

This support ticket is created 5 years, 8 months 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 Jerome 5 years, 8 months ago.

Assisted by: Waqar.

Author
Posts
#1266129

Tell us what you are trying to do?
I am trying to filter by category and date. I have followed another question by a user and it is working on the first filter. I have also used a short javascript to keep the selection in the select field when the filter reloads. This is functioning the first time around. On subsequent filtering, the URL query parameter is staying and then adding a second one. If the choice is different the second time then the select option stays the same as the previous selection. I added a reset button and this resets the categories selected but not the dates. The selection of filters and filtering is working fine, but the select input is not updating with the page reload. It would be good to get both of these sorted.

Here is the themes functions.php code addition:

// Code for custom date filtering.
function filtering_year_func($query, $view_settings) {
global $WP_Views;
if($WP_Views->current_view == 1274)
{
if(isset($_REQUEST['my-year'])&&$_REQUEST['my-year'])
{
$query['year'] = intval($_REQUEST['my-year']);
}
}
return $query;
}

add_filter('wpv_filter_query', 'filtering_year_func', 20, 2);

Here is the Javascript code embedded in the view to update the select option from the URL parameter

$(document).ready(function() {
// Construct URL object using current browser URL
var url = new URL(document.location);

// Get query parameters object
var params = url.searchParams;

// Get value of paper
var paper = params.get("my-year");

// Set it as the dropdown value
$("#my-year").val(paper);
});

There are two things I would like to get resolved:
1 - When you select a second date to filter, the second one is selected when the page reloads to filter the new results.
2 - The reset button should reset the page and clear all selections.

Is there any documentation that you are following? https://toolset.com/forums/topic/filtering-posts-by-category-and-year/

What is the link to your site? hidden link?

#1266715

Hi Jerome,

Thank you for waiting, while I performed a few tests on my website.

To fix both these issues, I'll suggest to remove the PHP code snippet for the filter "wpv_filter_query" and instead use a "Post date" filter, from the "Query Filter" section, as shown in this screenshot:
hidden link

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1266839

That worked a treat. Thanks so much for the quick reply.

All working fine now.