Year Range

By: Peter S | Asked: 03/21/2024
ForumsCategory: Code HelpYear Range
Peter S asked 8 months ago

Hi,

I have three fields:

Year of Birth - Stores the year of birth
Year - Use to search the years of birth
Range - to set a range for the search. For example if Year equals 1882 and the range is set to +/- 1 then it should give the result 1881, 1882 and 1883

I am using the attached code but it returns no results. Can anyone help identify the problem? 

Thanks!

Attachments
1 Answers
Rob LeVineRob LeVine answered 8 months ago

I suggest turning on WordPress debugging and putting in error_log statements to see what the values are at various points in the code.

Peter S replied 7 months ago

hey,
I tried that and I don't see any bugs.

Rob LeVineRob LeVine replied 7 months ago

It's not necessarily a bug you're looking for, it's more about whether the values are correct at every point during the process. I don't even recall what the code is and the link seems to be dead.

Peter S replied 7 months ago

This was the code

function custom_filter_entries($where, $args) {
error_log("Initial WHERE clause: " . $where);

$view_id = 55;
$year_field_id = 175;
$range_field_id = 178;

if ($args['display']->ID == $view_id && $args['where_opt'] == $year_field_id) {
$selected_range = isset($_GET['range']) ? sanitize_text_field(urldecode($_GET['range'])) : '';

error_log("Selected Range: " . $selected_range);

if (!empty($selected_range)) {
$central_year = intval($_GET['yearofbirth']);
$range_years = array();

if ($selected_range == "-/+ 1yrs") {
$range_years = array($central_year - 1, $central_year, $central_year + 1);
} elseif ($selected_range == "-/+ 2yrs") {
for ($i = -2; $i <= 2; $i++) {
$range_years[] = $central_year + $i;
}
}

$conditions = array();
foreach ($range_years as $year) {
$conditions[] = "meta_value LIKE '%" . esc_sql($year) . "%'";
}

$where = "(" . implode(" OR ", $conditions) . ")";
error_log("Modified WHERE clause: " . $where);
}
}

return $where;
}

add_filter('frm_where_filter', 'custom_filter_entries', 10, 2);

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right