Performant Unique Results (for Database Object Type)

Populate Anything limits its queries to the first 500 results for performance. When using the “Only Show Unique Results” setting, results are filtered down from those first 500 results meaning that if you’re looking for distinct values in a database that has more than 500 results, it’s possible that not all unique results will be returned.

The traditional solution for this is to increase the query limit to the maximum size of your database but this can come at a significant performance cost.

This snippet will check if the Order By, Value Template, and Label Template are all the same and if so, it will update the query to only select the distinct values from the Order By column. This will ensure that all unique results are returned without the performance cost of increasing the query limit.

Code

Filename: gppa-performant-unique-results.php

<?php
/**
 * Gravity Perks // Populate Anything // Performant Unique Results (for Database Object Type)
 * https://gravitywiz.com/documentation/gravity-forms-populate-anything/
 *
 * Populate Anything limits its queries to the first 500 results for performance. When using the "Only Show Unique Results"
 * setting, results are filtered down from those first 500 results meaning that if you're looking for distinct values in
 * a database that has more than 500 results, it's possible that not all unique results will be returned.
 *
 * The traditional solution for this is to increase the query limit to the maximum size of your database but this can
 * come at a significant performance cost.
 *
 * This snippet will check if the Order By, Value Template, and Label Template are all the same and if so, it will
 * update the query to only select the distinct values from the Order By column. This will ensure that all unique
 * results are returned without the performance cost of increasing the query limit.
 */
add_filter( 'gppa_object_type_database_query', function( $query, $args, $db_object_type ) {
	global $wpdb;

	$order_by       = rgars( $args, 'ordering/orderby' );
	$template_value = rgars( $args, 'templates/value' );
	$template_label = rgars( $args, 'templates/label' );
	$are_all_same   = count( array_unique( array( $order_by, $template_value, $template_label ) ) ) === 1;

	if ( $are_all_same ) {
		// %i is a valid placeholder as of WP 6.2
		// phpcs:ignore
		$query = str_replace( 'SELECT *', $wpdb->prepare( 'SELECT DISTINCT %i', $order_by ), $query );
	}

	return $query;
}, 10, 3 );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.