I have "rate_min" and "rate_max" post meta for "Sitter" post type.
I have a custom shortcode that (given a Sitter ID) displays the set amount plus 12% because that's what the rate is after fees.
So the actual database value of "rate_min" might be "10", but the front-end displays "11.2".
When a site visitor wants to find Sitters by their rate, such as rates between 11-15, the 11.2 one should show up. But if they search 8-11, the 11.2 one should not show up (even though the real value is 10).
The attached screenshot is what I have so far for the custom field filter.
Should I instead select the "Shortcode attribute" dropdown option instead of the "URL parameter"? I'm unclear what "Shortcode attribute" really is / how it would work.
If not, I'm okay filtering 'wpv_filter_query', but I don't want to duplicate what Views is already filtering (see screenshot). Is there an easy way to "add 12% to the value", even if via custom code, or is the only way to remove this custom field filter (from screenshot) and instead add the meta_query via custom code?
Thank you.
Hello,
Q1) Should I instead select the "Shortcode attribute" dropdown option instead of the "URL parameter"?
No, in your case, it should not use "Shortcode attribute", that is for pass parameter using shortcode attribute, see our document:
https://toolset.com/documentation/user-guides/passing-arguments-to-views/#controlling-the-filter-with-shortcode-attributes
Controlling the filter with shortcode attributes
Q2) I'm okay filtering 'wpv_filter_query', but I don't want to duplicate what Views is already filtering (see screenshot). Is there an easy way to "add 12% to the value",
Yes, it needs custom codes using filter hook "wpv_filter_query", for example:
add_filter('wpv_filter_query', function($args, $settings, $view_id){
if($view_id == 123 && isset($args['meta_query'])){
foreach($args['meta_query'] as $k => $v){
if(isset($v['key']) && $v['key'] == 'wpcf-rate'){
$arr = explode(',', $v['value']);
$arr[0] = $arr[0] * ( 1+ 0.12);
$str = implode(',', $arr);
$args['meta_query'][$k]['value'] = $str;
}
}
}
return $args;
}, 10, 3);
Please replace 123 with your view's ID
More help:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
Thank you very much.
A)
This is the actual code in the "Search and Pagination" section of the View:
<div class="form-group">
<label>[wpml-string context="wpv-views"]Sitter Min Hour Rate[/wpml-string]</label>
[wpv-control-postmeta field="wpcf-sitter-hour-rate" type="textfield" url_param="wpv-wpcf-sitter-hour-rate_min"]
</div>
<div class="form-group">
<label>[wpml-string context="wpv-views"]Sitter Max Hour Rate[/wpml-string]</label>
[wpv-control-postmeta field="wpcf-sitter-hour-rate" type="textfield" url_param="wpv-wpcf-sitter-hour-rate_max"]
</div>
However,
is never set even when I search with these fields.
B)
I really appreciate your snippet, but it looks like it doesn't take into account the min and max fields and their BETWEEN comparison (see initial screenshot).
Is there something wrong that $query['meta_query'] is never in use? Or should I just manually check $_REQUEST and add the meta query logic (including BETWEEN) myself, in which case, should I remove this field's rules from the "Custom field filter" settings?
Above codes works fine in my localhost, since in your screenshot:enlace oculto
It is using field slug "wpcf-rate", so I have only tested with custom numeric field "rate" in my localhost.
In your new post the field slug is "wpcf-sitter-hour-rate":
https://toolset.com/forums/topic/add-12-to-post-meta-value-before-searching-based-on-this-custom-field/#post-1186986
you can replace "wpcf-rate" with "wpcf-sitter-hour-rate".
For the new question:
Is there something wrong that $query['meta_query'] is never in use?
$query['meta_query'] will be in use only submit the form and pass URL parameter "wpv-wpcf-sitter-hour-rate_min" to view.
I suggest you debug your codes manually, line by line.
Ah, the issue was using the wpv_filter_query hook's priority 10... meta_query wasn't yet set... priority 50 worked.
However, it's not working as expected. From the attached screenshot... we're wanting to make sure a front-end search for a rate between "10" and "10" would match a sitter profile with a raw meta value of "8.93" -- so then why are there zero results? Using "type=NUMERIC" and "compare=BETWEEN" should make things match a raw value of 8.93
Thanks for any pointers.
Since it is a custom PHP codes problem, if you need more assistance for it, please provide a test site with the same problem, also point out the problem page URL and view URL, I need to test it in a live website
Figured it out... Views was set to "numeric" but using Query Monitor I realized it was CAST() as enlace oculto">SIGNED -- which is integers... and I was trying to compare decimals, so I changed to "decimal" in Views and it works now.
Thanks for your help, especially the starting code snippet.
Full code snippet for reference:
/**
* Toolset View #45 (Sitter Search): Filter the results by the hourly rate, adjusting from Sitter's raw value
* to the actual rate Parents will pay (adding 12%).
*
* @param $query
* @param $view_settings
* @param $id
*
* @return mixed
*/
public function sitter_search_adjust_rate_for_booking_fees( $query, $view_settings, $id ) {
if (
45 !== absint( $id )
|| ! isset( $query['meta_query'] )
) {
return $query;
}
foreach ( $query['meta_query'] as $k => $v ) {
if (
isset( $v['key'] )
&& $v['key'] == 'wpcf-sitter-hour-rate'
) {
$new = [];
$arr = explode( ',', $v['value'] );
foreach ( $arr as $a_key => $a_value ) {
$becomes = $a_value / 1.12;
if (
// BETWEEN Min to Max range and we're at Max
(
0 < count( $new )
&& 1 === $a_key
)
// Max without a Min
|| (
0 === count( $new )
&& ! empty( $query['meta_query'][$k]['compare'] )
&& '<=' === $query['meta_query'][$k]['compare']
)
) {
$becomes = $becomes + 0.01;
} else {
$becomes = $becomes - 0.01;
}
$new[] = $becomes;
}
$str = implode( ',', $new );
$query['meta_query'][$k]['value'] = $str;
}
}
return $query;
}
My issue is resolved now. Thank you!