Skip Navigation

[Resolved] Filter view by uniqueness of field value

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 matthewC-9 3 months ago.

Assisted by: Minesh.

Author
Posts
#2798395

Hi! I'm trying to figure out if there's a way to filter a view so that only one post with the same value in a given field will show. Specifically, I'm trying to set up a leaderboard where people can submit this info - name, email address, total points - and the entries will be displayed ordered by field = total points, descending. If someone wants to send in a later submission with a higher total points value, I would ideally like to be able to filter the view by field = email address, so that of all submissions with the same email address, only the one with the highest value in the total points field will show. Is that possible?

#2798428

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Based on the description you shared - it seems to be possible but if you can setup a test content and site and send me admin access details with problem URL where you want to display exactly what content that will help me to guide you in the right direction.

Once I review your current setup and content strcture that will give me idea what will be the best possible solution in your case.

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

#2798637

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

I've added the following code to "Custom Code" section offered by Toolset:
=> hidden link

add_filter( 'wpv_filter_query_post_process', 'func_groupby_unique_email_view_result', 10, 3 );
function func_groupby_unique_email_view_result( $query, $view_settings, $view_id ) {
     
  if ($view_id==11327 and !empty( $query->posts ) ) { 
           
      $result = array();
     
     foreach($query->posts as $k=>$v):
        $email = get_post_meta($v->ID,'wpcf-contest-email-address',true);
       
        if(!in_array($email,$result)){
           $final_result[]=$v;
           $result[]=$email;
        }
      
 
     endforeach;
        
        $query->posts = $final_result; 
        $query->found_posts = count($final_result); // modify the count of found posts
              $query->post_count = count($final_result); // modify the count of displayed posts
    }
    return $query;
}

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

#2799224

That seems to work very nicely - thank you, Minesh!