Passer la navigation

[Résolu] How to search multiple custom fields (number values) as a single query

This support ticket is created Il y a 1 year, 10 months. 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
- 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 -

Fuseau horaire du supporter : Asia/Kolkata (GMT+05:30)

Ce sujet contient 6 réponses, a 2 voix.

Dernière mise à jour par Minesh Il y a 1 year, 10 months.

Assisté par: Minesh.

Auteur
Publications
#2708503
old-screenshot-properties.retailspecialists.com-2024.07.19-18_09_12.png
screenshot-retailspecialists.net-2024.07.19-18_05_21.png
screenshot-retailspecialists.net-2024.07.19-18_07_43.png

Tell us what you are trying to do?
I would like to build a search feature allowing users to specify a range (square footage as number), which can pull from several custom fields (space 1, space 2, space 3).

I have done 2 things, but can't seem to figure out the next step, so they duplicate each other:
1. the first is to attempt a 'repeatable field' called Space size, and one of the repeating fields is 'Size SF' (then all square footage values have the same name) but I am stumped when I realized they are post-types with a relation. (In the search I cannot just set a 'between' value range to narrow down search parameters, like I can with non-repeatable fields.)
-- If I go this route it seems I would set up a second 'view', and feed it into the first view?

2. The second is to set up multiple fields, named 'space 1', 'space 2', ... to 'space 8'. Each can have a numeric square footage, but search queries tend to focus on just one custom field ('space 1' OR 'space 2') instead of a cluster of vlaues ('space 1' 'space 2' 'space 3') since each space may fit the search criteria.

Is there any documentation that you are following?
I have looked at 4 or 5 support tickets on toolset. Mostly they seem to deal with text fields having multiple queries.

Is there a similar example that we can see?
Major Commercial real estate sites like "loopnet" have a range filter (price and size) for a property. lien caché
Our old website has this feature as well: (go to 'more filters,' and it can search by 'min or max space.')
lien caché

I am currently trying to rebuild site, but I am having trouble with search refinement.
lien caché
(scroll down to see my tests)

What is the link to your site?
lien caché

#2708664

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

What if you try to follow the same steps as given with the following Doc:
- https://toolset.com/lesson-placement/lesson-placements-1824669-1621213/#1-creating-the-custom-search-form

- There should be only one field "Space SF" that should hold the number value for every post and then you should add the custom field filter to your view as shown with the above doc.
- Once you add the custom search field for "Space SF" on right sidebar you should select "Field Settings" and chose comparison "Between" that will offer you the min and max input range.

Do you able to configure that - is that the thing you were looking for?

#2708796

Minesh, Than you for your response.
I can do what you suggested very easily for the other custom field number values, such as "lot size" or "available size". That is no problem.

My problem is I have to have multiple Square Ft (Space SF) for many of the posts: If a property has 3 vacancies, each one will have different square footage. (example: lien caché has 7 spaces, each with different square feet.) So, I cannot have just one Space SF value.

Does that make sense? If you see in my previous screenshot, I am trying to create *either* a repeatable field or several fields, but I am having trouble integrating the multiple values (from multiple fields) into the custom search.

#2708973

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

My problem is I have to have multiple Square Ft (Space SF) for many of the posts: If a property has 3 vacancies, each one will have different square footage. (example: lien caché has 7 spaces, each with different square feet.) So, I cannot have just one Space SF value.
====>
In that case what if you create a custom field "Spaces" and make it repeating field.
- https://toolset.com/course-lesson/creating-custom-fields/#making-individual-fields-repeatable

Will that help? or you require repeating field group?

#2709254

Minesh,
yes, the first question (and you can see in the screenshot), I have repeatable fields called spaces, with a field called "space sf" that is a value (number).

My problem is how to search those repeatable fields INSIDE the larger property custom search.
Repeatable fields creates a post-relation, so it will create a type of post called "Spaces" and the repeating field will be "space sf" and "space type".
*Example:* Legacy Park (the property) has 7 spaces. Each space has a different square footage (space 1 is 1200, space 2 is 1400, space 3 is 1600, etc... ) _ I built a view that can search for this: type in 2,000 to 4,000 sf and if will show the correct properties. (it is on the homepage at the bottom: lien caché )

What I cannot figure out is how to get the "Repeatable Field search" and View into the larger, map based, custom field view at the top. Perhaps a view that combines both?

VIDEO Screen walkthrough: lien caché

#2709287

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

I was suggesting repeating (multiple instance) field (not repeating field group) - but with that you can only store the sq. ft as number not the type, if you want to deal with multiple fields like space type and "space sf" - you need to go with repeating field group.

So, do you want to keep "Space Type" and "Space SF" fields - then you will have to go with repeating field group.

If you want only "Space SF" that holds the number - then we can go with repeating field.

Please clarify this and send me admin access details and problem URL.

*** 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.

#2710570

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Can you please check now: lien caché

I've added the following view's filter hook "wpv_filter_query" to "Custom Code" section offered by Toolset:
=> lien caché

add_filter( 'wpv_filter_query', 'func_search_rfg_custom_field_return_parent', 10, 3 );
function func_search_rfg_custom_field_return_parent( $query, $view_settings, $view_id ) {
    
  if ( $view_id == 3338 ) { 
    
    $meta_query = $query['meta_query'];
    foreach($meta_query as $k=>$v):
    	if(isset($v['key']) and $v['key']=='wpcf-available-size'){
          unset($query['meta_query'][$k]);
        }
    endforeach;
    
   
    
    $parent_ids = array();
    if (isset($_GET['wpv-wpcf-available-size_min']) and $_GET['wpv-wpcf-available-size_min']!='' and
        isset($_GET['wpv-wpcf-available-size_max']) and $_GET['wpv-wpcf-available-size_max']!='' ) {
      
      $rfg_slug='space-size';
      $rfg_field_name = "wpcf-space-sf";
       
      /// getting  sf size from URL param
     $min_space_sf = $_GET['wpv-wpcf-available-size_min']; 
     $max_space_sf = $_GET['wpv-wpcf-available-size_max']; 
      
      /// finding rfg posts within defiend range
      $args = array(
                    'post_type'=> $rfg_slug,
                    'meta_query' => array(
                                array('key' =>  $rfg_field_name,
                                    'type' => 'NUMERIC',
                                    'compare' => 'BETWEEN',
                                    'value' => array($min_space_sf,$max_space_sf)
                                ),
                               )
                );
      
        $range_found_posts = get_posts( $args );
        $all_posts = $range_found_posts;
        foreach($all_posts as $k=>$v):
           /// gettting related parent
           $get_parent =  toolset_get_related_post($v->ID, $rfg_slug);
            if(!in_array($get_parent,$parent_ids)){
                    $parent_ids[$get_parent] =$get_parent;
             } 
              endforeach;
       
      $query['post__in'] = (!empty($parent_ids))?$parent_ids:0;
          
             
         
                
    }        
  }
    return $query;
}

More info:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

Can you please confirm it works as expected now.

#2710630

WOW! thank you so much! that works perfectly