Skip Navigation

[Resolved] Filter custom post by

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to create a custom search filter that lets me filter posts by "age", calculated from a date in a custom field. I would like to be able to select an age range in my custom search View.

If I must filter by date range instead, I would like to show more than just 20 years in the datepicker's "year" select field.

Solution: Views can let you set up a custom search filter between two values, but those values must be custom field values or the post date. They cannot be values calculated on-the-fly. "Age" is not a custom field value - it depends on a calculation using the current date. So there is not a good way to set that up in the Views GUI. I can help you set up filters between two custom field dates, but not between two ages. That would require a significant amount of custom code that falls outside the scope of the support we provide here in the forums.

If you are comfortable writing your own code, you can use the wpv_filter_query API filter to apply a postmeta value range based on your own custom inputs. You could access the custom input values in PHP in the $_GET superglobal, and use those to calculate a start and end timestamp. Then you could apply those criteria to a meta query on your birthdate field.

If you want to filter by date range using datepickers, you can control the year range using the standard jQuery UI option "yearRange":

jQuery(document).ready(function(){
  jQuery('input.hasDatepicker').datepicker('option',{'yearRange':'1990:2040'});
});

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
More info about WordPress Meta Query here:
https://codex.wordpress.org/Class_Reference/WP_Meta_Query
http://api.jqueryui.com/datepicker/#option-yearRange

This support ticket is created 6 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by culturaI 6 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#603971

Hello i'm trying to filter my custom posts from age between 2 numbers. For example age between 30 and 50.
My site: hidden link

I can correcly display age from date, i added code found here in my function.php:

add_shortcode( 'time_ago', 'time_ago_func');
function time_ago_func($atts){
global $wpdb;
extract( shortcode_atts( array(
'birthdate' => '',
), $atts ) );

//explode the date to get month, day and year
//$birthDate = explode("-", $birthDate);
//get age from date or birthdate
//$age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md") ? ((date("y")-$birthDate[2])-1):(date("Y")-$birthDate[2]));
$age = floor((time() - $birthdate)/(365*24*60*60));
return $age;
}

In VIEW i used this shortcode, found here:
[time_ago birthdate='[types field="data-di-nascita" raw="true"][/types]']

Until now everything is working good, but i can't understand how to add this particular search on filter editor and query section.

I would like help.

Thanks

#604109

Views can let you set up a custom search filter between two values, but those values must be custom field values or the post date. They cannot be values calculated on-the-fly. "Age" is not a custom field value - it depends on a calculation using the current date. So there is not a good way to set that up in the Views GUI. I can help you set up filters between two custom field dates, but not between two ages. That would require a significant amount of custom code that falls outside the scope of the support we provide here in the forums.

If you are comfortable writing your own code, you can use the wpv_filter_query API filter to apply a postmeta value range based on your own custom inputs. You could access the custom input values in PHP in the $_GET superglobal, and use those to calculate a start and end timestamp. Then you could apply those criteria to a meta query on your birthdate field. More information about our API can be found here:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
More info about WordPress Meta Query here:
https://codex.wordpress.org/Class_Reference/WP_Meta_Query
Let me know if you need additional assistance with the wpv_filter_query API and I will be glad to help.

#604581

Thanks Christian i understand but i'm not too confortable to writing my code, so for now i will simply filter normal date.

I could use some help to set my date filter because i have a little problem:

I can set on my view filter between two dates, but when i click to search on frontend i can display only date 10 years ago (2008). If i click on 2008 i can display until 1998, ecc...

I don't know if it's clear, so you can look to my link : hidden link

#604713

Views uses jQuery UI datepickers, and you can use the "yearRange" option to specify the start and end years shown in the year select field. To set both datepickers with the same start and end years, add this JS code to your View Filter Editor JS panel:

jQuery(document).ready(function(){
  jQuery('input.hasDatepicker').datepicker('option',{'yearRange':'1990:2040'});
});

This will show the years 1990 - 2040. You can modify that range to fit your requirements. The yearRange option is documented here: hidden link

#604845

Thanks Christian worked great!

The forum ‘Types Community Support’ is closed to new topics and replies.

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