Skip Navigation

[Resolved] Custom field search filters are getting conflicts.

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 15 replies, has 2 voices.

Last updated by Minesh 8 months, 2 weeks ago.

Assisted by: Minesh.

Author
Posts
#2687268

jum

Tell us what you are trying to do?
I have two custom fields Estimated total amount and Grants status these two field are working fine independently but when i use two filter at the same time then the estimated total amount is not working.

Grant status have dropdown (open,closed,all), i uses a custom php code that compare final closing date
custom field with the current date.

ex: status -> open
show Final closing date is greater than or equal to today
status-> closed
show final closing date is lesser than today
all-> clear filter

Estimated total amount is set as numeric Value between Min and Max.

The estimated field is working when grant status is not provided else its not working.

Is there a similar example that we can see?
I use classic editor for the view .
hidden link

Please guide us how to make this two filter working together.

Thanks in advance,

#2687364

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I will have to check how you setup the view with query filter.

Can you please share details what filters I should select and what should be your expected results?

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2687407

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I search for the "open" grant:
- hidden link

I see the first result is "Project Grants (social sciences | Sweden) Copy", but when I edit the same post:
- hidden link

The Grant status for above post is "closed".

I hope this is not correct - and you want to display the posts with Grant status equal to "open" and the custom field "grant-final-closing-date" value is greater than current time - is that correct?

#2687423

jum

Hi Minesh,

Yes you are correct want to display the posts when Grant status equal to "open" and the custom field "grant-final-closing-date" value is >= than current date.

I created this Grant status select dropdown and set the option as No default but when i checked now it showing grant status closed for all the grants and I havent manually provided the grant status to the any grant post. I tried deleting the grant status custom field now and created the new custom field(grant status updated) with no value as default but it taking as status closed by default in all the grants. (the new custom field name is not updated in the code yet) .

Not sure why it automatically taking as closed in custom field. Do i need to set grant status as a seperate custom code drop down to work with this logic?

I do have a page with grant status as ajax with custom dropdown and same php for open and closed. (In block editor )
The same issue the total estimated amount (normal toolset custom field) work seperate and grant status work seperate. grant status work well with taxonomy filters only issue is with this estimated total amount. That´s the reason i tried creating status custom field to make it logic work together.

hidden link
view name: 2023RFD

code snippet name: dropdown -open or closed
<form method="get" action="<?php echo esc_url(add_query_arg($_GET)); ?>">
<label for="grant_status">Grant Status</label><br />
<select name="grant_status" id="grant_status">
<option value="all" <?php echo (isset($_GET['grant_status']) && $_GET['grant_status'] === 'all') ? 'selected' : ''; ?>>All</option>
<option value="open" <?php echo (isset($_GET['grant_status']) && $_GET['grant_status'] === 'open') ? 'selected' : ''; ?>>Open Grants</option>
<option value="closed" <?php echo (isset($_GET['grant_status']) && $_GET['grant_status'] === 'closed') ? 'selected' : ''; ?>>Closed Grants</option>
</select><br />
<!-- Include hidden inputs to append other existing parameters -->
<?php
foreach ($_GET as $key => $value) {
if ($key !== 'grant_status') {
echo '<input type="hidden" name="' . htmlspecialchars($key) . '" value="' . htmlspecialchars($value) . '">';
}
}
?>
<input type="submit" name="submit_status" value="Filter"><br />
<?php
// current query parameters
$current_url = strtok($_SERVER["REQUEST_URI"], '?');
$query_params = $_GET;

// Remove 'grant_status' from existing query parameters
unset($query_params['grant_status']);

// The new URL with updated 'grant_status' parameter
$new_url = $current_url . '?' . http_build_query($query_params);
?>
</form>
<script src="hidden link"></script>
<script>
$(document).ready(function() {
$('#grant_status').on('change', function() {
// Get the form element
var form = $(this).closest('form');

// 'submit_status' parameter
var submitStatusInput = $('<input type="hidden">');
submitStatusInput.attr('name', 'submit_status');
submitStatusInput.val('Filter');
form.append(submitStatusInput);
form.submit();
});
});
</script>

<style>
/* filter button visibility */
input[name="submit_status"] {
display: none;
}
</style>
---------------------------------------------------------------
Php snippet name: open and closed old

function set_grant_status_filter3($query_args, $view_settings, $view_id) {
if ($view_id == 43949) {
// Grant status filter using URL parameters
if (isset($_GET['submit_status']) && isset($_GET['grant_status'])) {
$grant_status = sanitize_text_field($_GET['grant_status']);
$current_timestamp = time();

// query filter for taxonomy take
$tax_query = array(
array(
'taxonomy' => 'rolling',
'field' => 'slug',
'terms' => 'yes',
'operator' => 'NOT IN',
),
);

// Filterfor grant status
if ($grant_status == 'open') {
$query_args['meta_query'] = array(
'relation' => 'OR',
array(
'key' => 'wpcf-grant-final-closing-date',
'value' => $current_timestamp,
'type' => 'NUMERIC',
'compare' => '>=',
),
array(
'key' => 'wpcf-grant-final-closing-date',
'value' => '',
'compare' => '='
)
);

} elseif ($grant_status == 'closed') {
$query_args['meta_query'] = array(
array(
'key' => 'wpcf-grant-final-closing-date',
'value' => $current_timestamp,
'type' => 'NUMERIC',
'compare' => '<',
),
array(
'key' => 'wpcf-grant-final-closing-date',
'value' => '',
'compare' => '!='
)
);

// $query_args['tax_query'] = $tax_query;
}
}

// Pagination
global $wp_query;
if (isset($_GET['wpv_paged'])) {
$wp_query->query_vars['paged'] = intval($_GET['wpv_paged']);
} else {
$wp_query->query_vars['paged'] = 1;
}

}
return $query_args;
}

add_filter('wpv_filter_query', 'set_grant_status_filter3', 10, 3);

-----------------------------------------------------
Trying to do the same in both view to make this logic work.

Please guide me how to set this grant status appropriate to work along with other filters.
Thanks in advance,

#2687601

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes you are correct want to display the posts when Grant status equal to "open" and the custom field "grant-final-closing-date" value is >= than current date.

I created this Grant status select dropdown and set the option as No default but when i checked now it showing grant status closed for all the grants and I havent manually provided the grant status to the any grant post. I tried deleting the grant status custom field now and created the new custom field(grant status updated) with no value as default but it taking as status closed by default in all the grants. (the new custom field name is not updated in the code yet) .

Not sure why it automatically taking as closed in custom field. Do i need to set grant status as a seperate custom code drop down to work with this logic?
====>
Ok.

So, with your custom field group where you added the new custom field "Grant status updated":
- hidden link

I've updated the values for the custom field options as you can see with the following screenshot:
- hidden link

If you edit any Grant post now, for instance:
- hidden link

You will see default option set as "Please Select" - does this makes sense? Do you see it working as expected. If yes:
- What is your next requirement or question? What about the old Grant status field? Do we have to count that field while displaying the result with view?

#2687657

jum

Hi ,

I have added this Grant status updated in field group of grants its at the last. Yes that changes you made in custom field is fine.

- What is your next requirement or question? What about the old Grant status field? Do we have to count that field while displaying the result with view?
Old grant status is deleted so no need to worry about it.

Grant status updated field need to show result based on estimated total amount min and max value.

Ex : When min = 10 and max= 20 it show result to 2 grants
when grant status is open it show result 329

expected output is: 2 result found when we use grant status-> open and min =10 and max=20

I tried to change the php code for open that take min and max value but it showing no result found

if ($grant_status == 'open') {
// Retrieve min and max values if provided
$min_amount = isset($_GET['wpv-wpcf-total-amount_min']) ? intval($_GET['wpv-wpcf-total-amount_min']) : '';
$max_amount = isset($_GET['wpv-wpcf-total-amount_max']) ? intval($_GET['wpv-wpcf-total-amount_max']) : '';

// Meta query array
$query_args['meta_query'] = array(
'relation' => 'AND',
array(
'key' => 'wpv-wpcf-total-amount',
'value' => array($min_amount, $max_amount),
'type' => 'NUMERIC',
'compare' => 'BETWEEN',
),
array(
'relation' => 'OR',
array(
'key' => 'wpcf-grant-final-closing-date',
'value' => $current_timestamp,
'type' => 'NUMERIC',
'compare' => '>=',
),
array(
'key' => 'wpcf-grant-final-closing-date',
'value' => '',
'compare' => '='
)
)
);

}

Please guide me how to make these two filters work together
Thanks

#2687872

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Sorry - its bit confusing now.

On what page I should try to implement the solution?
- hidden link
OR
- hidden link

#2687873

jum

Hi Minesh,

This page hidden link
view name: Grant database view 2024

Thanks

#2687879

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I set the "Grant status updated" field value to "open" for the following post(s):
- hidden link
- hidden link

With your code snippet - I've adjusted the code as given under:
- hidden link

function set_grant_status_filter1($query_args, $view_settings, $view_id) {
    if ($view_id == 94399) {
        	
		// Grant status filter using URL parameters
        if (isset($_GET['wpv-wpcf-grant-status-updated'])) {
            $grant_status = sanitize_text_field($_GET['wpv-wpcf-grant-status-updated']);
            $current_timestamp = time(); 

            //  query filter for taxonomy take
            $tax_query = array(
                array(
                    'taxonomy' => 'rolling',
                    'field'    => 'slug', 
                    'terms'    => 'yes', 
                    'operator' => 'NOT IN',
                ),
            );

            // Filter for grant status
            if ($grant_status == 'open') {
                $query_args['meta_query'][] = array(
                    'relation' => 'OR',
                    array(
                        'key' => 'wpcf-grant-final-closing-date', 
                        'value' => $current_timestamp, 
                        'type' => 'NUMERIC',
                        'compare' => '>=', 
                    ),
                    array(
                        'key' => 'wpcf-grant-final-closing-date',
                        'value' => '', 
                        'compare' => '='
                    )
                );
                
            } elseif ($grant_status == 'closed') {
                $query_args['meta_query'][] = array(
                    array(
                        'key' => 'wpcf-grant-final-closing-date', 
                        'value' => $current_timestamp, 
                        'type' => 'NUMERIC',
                        'compare' => '<', 
                    ),
                    array(
                        'key' => 'wpcf-grant-final-closing-date',
                        'value' => '', 
                        'compare' => '!='
                    )
                );

            }
        }
		

/*
        // Check if "all" is selected in the dropdown
        if (isset($_GET['wpv-wpcf-grant-status-updated']) && $_GET['wpv-wpcf-grant-status-updated'] == 'all') {
            // "All" selected, show all results
            unset($query_args['meta_query']);
        }
*/
        // Pagination
        global $wp_query;
        if (isset($_GET['wpv_paged'])) {
            $wp_query->query_vars['paged'] = intval($_GET['wpv_paged']);
        } else {
            $wp_query->query_vars['paged'] = 1; 
        }
    }
    return $query_args;
}

add_filter('wpv_filter_query', 'set_grant_status_filter1', 10, 3);

If you can check now:
- hidden link

I've selected the "Grant status updated" field value to "open" and total-min=10 and total-max=20 and I can see two results displayed with the above link.

Can you please confirm it works as expected now.

#2688130

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check my last reply and try to resolve your issue.

#2688135

jum

Hi Minesh,

I have checked and noticed that as you set the two grants as open. The grant status filter works based on grant status updated custom field value in the post not based on Grant final closing date.

Ex: when you filter grant status to open it need to show result 307 found but it show only 2 as we set the custom field value is open for grants.

closed-> no result found. but it should be 2071.

Grant status open /closed filter result is only to display result based on final closing date . We are not manually updating the custom Grant status updated custom field in the post directly.

Please suggest me do i need to set this grant status field as a custom html dropdown instead of using as a custom field in the post to avoid confusion.

Kindly guide me in this scenarios

Thanks in advance,

#2688138

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now.

I've adjusted the code added to code snippet as given under:
- hidden link

function set_grant_status_filter1($query_args, $view_settings, $view_id) {
    if ($view_id == 94399) {
        
		
		// Grant status filter using URL parameters
        
        if (isset($_GET['wpv-wpcf-grant-status-updated'])) {
            $grant_status = sanitize_text_field($_GET['wpv-wpcf-grant-status-updated']);
            $current_timestamp = time(); 

            //  query filter for taxonomy take
            $tax_query = array(
                array(
                    'taxonomy' => 'rolling',
                    'field'    => 'slug', 
                    'terms'    => 'yes', 
                    'operator' => 'NOT IN',
                ),
            );

            // Filter for grant status
            if ($grant_status == 'open') {
                $query_args['meta_query'][] = array(
                    'relation' => 'OR',
                    array(
                        'key' => 'wpcf-grant-final-closing-date', 
                        'value' => $current_timestamp, 
                        'type' => 'NUMERIC',
                        'compare' => '>=', 
                    ),
                    array(
                        'key' => 'wpcf-grant-final-closing-date',
                        'value' => '', 
                        'compare' => '='
                    )
                );
                
            } elseif ($grant_status == 'closed') {
                $query_args['meta_query'][] = array(
                    array(
                        'key' => 'wpcf-grant-final-closing-date', 
                        'value' => $current_timestamp, 
                        'type' => 'NUMERIC',
                        'compare' => '<', 
                    ),
                    array(
                        'key' => 'wpcf-grant-final-closing-date',
                        'value' => '', 
                        'compare' => '!='
                    )
                );

            }
        }
		
		
		
		$args = $query_args['meta_query'];
		if(!empty($args)) {	
				$found_key = array_search('wpcf-grant-status-updated', array_column($args, 'key'));
				unset($query_args['meta_query'][$found_key]);
				
		}
		
		
/*
        // Check if "all" is selected in the dropdown
        if (isset($_GET['wpv-wpcf-grant-status-updated']) && $_GET['wpv-wpcf-grant-status-updated'] == 'all') {
            // "All" selected, show all results
            unset($query_args['meta_query']);
        }
*/
        // Pagination
        global $wp_query;
        if (isset($_GET['wpv_paged'])) {
            $wp_query->query_vars['paged'] = intval($_GET['wpv_paged']);
        } else {
            $wp_query->query_vars['paged'] = 1; 
        }
    }
    return $query_args;
}

add_filter('wpv_filter_query', 'set_grant_status_filter1', 10, 3);

#2688426

jum

Hi Minesh,

Thanks for quick support.

Ya it show the result expected when open/ closed are provided . When i checked Estimated total amount with out other queries then the result is not working. the estimated total amount working only when open/ closed are provided not when i give only total amount.

Ex: in a filter if i give estimated min= 10 and Max = 20 the result is 2378 instead of 2 .
when i give status to open and Min=10 and Max=20 then it show result 2

seems like Estimated total amount filter is working only when the grant status is provided.

Please guide me to resolve this
Thanks in advance,

#2688432

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ex: in a filter if i give estimated min= 10 and Max = 20 the result is 2378 instead of 2 .
when i give status to open and Min=10 and Max=20 then it show result 2
=====>

Here is the updated code:

function set_grant_status_filter1($query_args, $view_settings, $view_id) {
    if ($view_id == 94399) {
        
		
		// Grant status filter using URL parameters
        $grant_status = '';
        if (isset($_GET['wpv-wpcf-grant-status-updated'])) {
            $grant_status = sanitize_text_field($_GET['wpv-wpcf-grant-status-updated']);
		}
            $current_timestamp = time(); 

            //  query filter for taxonomy take
            $tax_query = array(
                array(
                    'taxonomy' => 'rolling',
                    'field'    => 'slug', 
                    'terms'    => 'yes', 
                    'operator' => 'NOT IN',
                ),
            );

            // Filter for grant status
            if ($grant_status == 'open') {
                $query_args['meta_query'][] = array(
                    'relation' => 'OR',
                    array(
                        'key' => 'wpcf-grant-final-closing-date', 
                        'value' => $current_timestamp, 
                        'type' => 'NUMERIC',
                        'compare' => '>=', 
                    ),
                    array(
                        'key' => 'wpcf-grant-final-closing-date',
                        'value' => '', 
                        'compare' => '='
                    )
                );
                
            } elseif ($grant_status == 'closed') {
                $query_args['meta_query'][] = array(
                    array(
                        'key' => 'wpcf-grant-final-closing-date', 
                        'value' => $current_timestamp, 
                        'type' => 'NUMERIC',
                        'compare' => '<', 
                    ),
                    array(
                        'key' => 'wpcf-grant-final-closing-date',
                        'value' => '', 
                        'compare' => '!='
                    )
                );

            }	
		$args = $query_args['meta_query'];
		if(!empty($args) and isset($_GET['wpv-wpcf-grant-status-updated']) and $_GET['wpv-wpcf-grant-status-updated']!='') {	
			                               
				$found_key = array_search('wpcf-grant-status-updated', array_column($args, 'key'));
				unset($query_args['meta_query'][$found_key]);
		}
		
/*
        // Check if "all" is selected in the dropdown
        if (isset($_GET['wpv-wpcf-grant-status-updated']) && $_GET['wpv-wpcf-grant-status-updated'] == 'all') {
            // "All" selected, show all results
            unset($query_args['meta_query']);
        }
*/
        // Pagination
        global $wp_query;
        if (isset($_GET['wpv_paged'])) {
            $wp_query->query_vars['paged'] = intval($_GET['wpv_paged']);
        } else {
            $wp_query->query_vars['paged'] = 1; 
        }
    }
    return $query_args;
}

add_filter('wpv_filter_query', 'set_grant_status_filter1', 10, 3);

You can adjust the above code as per your requirement as you do know know where to edit the code and what edits it should require as per your conditional requirement you can add/remove query arguments dynamically using the above filter hook.

#2688456

jum

Hi Minesh,

Thanks for the quick changes, The filter for the grants are working fine now with other filters

I have one more question related to filters.
Grant post type(child) have many to many relationship with Funder post type(Parent). The Funder post type have a Taxonomy Location.

When I tried to add Funder location taxonomy filter in grant search filter . in filter i choose post relationship or repeatable field group owner . In relationship i select Funders(in funder grant connection) it dont show option to select the taxonomy.

Please guide how to give parent taxonomy in child search filter.

Thanks in advance,