Skip Navigation

[Resolved] How to display the number of records by search filter

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 0 reply, has 1 voice.

Last updated by Rostislav Avtodiychuk 1 month, 2 weeks ago.

Assisted by: Minesh.

Author
Posts
#2804025

I've been asking this question for a long time
https://toolset.com/forums/topic/how-to-display-the-number-of-records-by-search-filter/ ,
sorry for the long answer. But this problem needs to be solved, I created a test site so you can have access to it.
I'll duplicate the question below:
Tell us what you are trying to do? I have view - list of people with 5 types custom fields in custom types with checkboxes yes/no and they uses in search filter in view. Can i show on separate page - statistic by with this 5 types filter in view - how many people have Yes or No
1 Type: Yes - (number) people;
1 Type:- No - (number) people;
....................
5 Type: Yes - (number) people;
5 Type: No - (number) people;

I add name of my custom field but it return me value only for first type (field). Each other fields is 0. I use "text-field" in block editor
1 Type: Yes - 73people;
1Type:- No - 0 people;
2 Type: Yes - 0 people;
2 Type: No - 0 people;
3 Type: Yes - 0 people;
3 Type: No - 0 people;
4 Type: Yes - 0 people;
4 Type: No - 0 people;
5 Type: Yes - 0 people;
5 Type: No - 0 people;

Thank you for help

#2804037

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Do you want custom search view or you just want to display yes/no count for every type?

Can you please share problem URL and admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) 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.

#2804079

Minesh
Supporter

Languages: English (English )

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

First of all, I've enable the legacy view.
- https://toolset.com/course-lesson/enabling-legacy-version-of-toolset-views/

I've created the following view that should display the total count using the shortcode:
- hidden link

[wpv-found-count]

Now, we can reuse the same view for different fields and values, having said that we are filtering the view results (in your case its total count) using view's shortcode attribute. For example"

[wpv-view name="View A" sector="sector-a"]
[wpv-view name="View A" sector="sector-b"]

For that - I've added the following vieww's filter hook "wpv_filter_query" to "Custom Code" section offered by Toolset with the code snippet namely "toolset-custom-code":
=> hidden link

add_filter('wpv_filter_query','func_show_total_checkbox_count_shortcode_argument', 99, 3);
function func_show_total_checkbox_count_shortcode_argument($query_args, $view_settings,$view_id) {
    // Check if it's the correct View
    if ($view_id == 2108) {
         
        global $WP_Views;
       	
      	  $field_slug = $field_value = '';
        
      	if(isset($WP_Views->view_shortcode_attributes[0]['field_slug'])){
          	 $field_slug = $WP_Views->view_shortcode_attributes[0]['field_slug'];
        }
        if(isset($WP_Views->view_shortcode_attributes[0]['field_value'])){
          	 $field_value = $WP_Views->view_shortcode_attributes[0]['field_value'];
        }
         
         $target_field = 'wpcf-'.$field_slug;
      
      	if(!empty($field_value) and !empty($field_value)) {
      
          $query_args['meta_query'][] = array(
                                      'key' => $target_field,
                                      'value' => $field_value,
                                      'type' => 'NUMERIC',
                                      'compare' => '='
                                      );
        }else if(empty($field_value)){
           $query_args['meta_query'][] = array(
                                      'key' => $target_field,
                                    	'compare' => 'NOT EXISTS'
                                      );
        
        }
         
         
    }
    return $query_args;
}


I've created the following test page and added the view's shortcode as given under with view's shortcode argument "field_slug" and "field_value":
- hidden link

Бзвп - YES: <span> [wpv-view name="show-total-count" cached="off" field_slug='potrebuye-bzvp' field_value='1']  </span>

Бзвп - NO: <span> [wpv-view name="show-total-count" cached="off" field_slug='potrebuye-bzvp']  </span>

ФП- YES: <span> [wpv-view name="show-total-count" cached="off" field_slug='potrebuye-fp' field_value='1']  </span>


ФП- NO: <span>  [wpv-view name="show-total-count" cached="off" field_slug='potrebuye-fp']  </span>

Where:
- we are passing the custom field slug to view's shortcode attribute name "field_slug"
- we are passing the custom field slug to view's shortcode attribute name "field_value"

As you can see it displays correct total post count on frontend:
- hidden link

You can add more views like this for your other field values.

For exaple:
- To display Yes count:

 [wpv-view name="show-total-count" cached="off" field_slug='your-field-slug' field_value="your-field-value"]

- To display NO count:

 [wpv-view name="show-total-count" cached="off" field_slug='your-field-slug']

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

I hope all above information will help you to resolve your issuel

#2804102

Thanks for the help, everything works, but what's the best way to do it if I have several post types people, people 1, people 2, how do I create separate statistics for them? Do I need to write a custom code for each and specify the view ID and create a view for each? Or can I write the view for each post type in the custom code separated by a comma so as not to create several custom codes?

#2804338

Minesh
Supporter

Languages: English (English )

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

In that case, if you also want to pass the post type, you can use the same view and I've modified code which I already added to "Custom Code" section offered by Toolset:
=> hidden link

add_filter('wpv_filter_query','func_show_total_checkbox_count_shortcode_argument', 99, 3);
function func_show_total_checkbox_count_shortcode_argument($query_args, $view_settings,$view_id) {
    // Check if it's the correct View
    if ($view_id == 2108) {
         
        global $WP_Views;
       	
      	 $post_type = $field_slug = $field_value = '';
        
      if(isset($WP_Views->view_shortcode_attributes[0]['post_type'])){
          	 $post_type = $WP_Views->view_shortcode_attributes[0]['post_type'];
       }
      
      	if(isset($WP_Views->view_shortcode_attributes[0]['field_slug'])){
          	 $field_slug = $WP_Views->view_shortcode_attributes[0]['field_slug'];
        }
        if(isset($WP_Views->view_shortcode_attributes[0]['field_value'])){
          	 $field_value = $WP_Views->view_shortcode_attributes[0]['field_value'];
        }
         
      
      	if(!empty($post_type)){
          $query_args['post_type'] = array($post_type);
        }
      	      
        
          
         $target_field = 'wpcf-'.$field_slug;
      
      	if(!empty($field_value) and !empty($field_value)) {
      
          $query_args['meta_query'][] = array(
                                      'key' => $target_field,
                                      'value' => $field_value,
                                      'type' => 'NUMERIC',
                                      'compare' => '='
                                      );
        }else if(empty($field_value)){
           $query_args['meta_query'][] = array(
                                      'key' => $target_field,
                                    	'compare' => 'NOT EXISTS'
                                      );
        
        }
         
         
    }
    return $query_args;
}


And you can call the different post types as:

Бзвп - YES: <span> [wpv-view name="show-total-count" cached="off" field_slug='potrebuye-bzvp' field_value='1' post_type='people'] </span>

Бзвп - NO: <span> [wpv-view name="show-total-count" cached="off" field_slug='potrebuye-bzvp' post_type='people'] </span>

ФП- YES: <span> [wpv-view name="show-total-count" cached="off" field_slug='potrebuye-fp' field_value='1' post_type='people'] </span>

ФП- NO: <span> [wpv-view name="show-total-count" cached="off" field_slug='potrebuye-fp' post_type='people'] </span>

YOu can change the "post_type" shortcode attribute value as required.

#2804373

Hello, Minesh thenk you for answer i'll created other view for some post type (they have same custom fields ), change you construction as
// Check if it's the correct View
if ($view_id == 2037 || $view_id == 2463 || $view_id == 2464 || $view_id == 2465 || $view_id == 2466 || $view_id == 2467) {
and it work for other Post Type

#2804374

Very qualified help! I never cease to be amazed at how convenient the toolset is for quickly creating professional websites of almost any type. Thanks to the developers and everyone who works on improvements.