Filter uncanny-automator

automator_get_users_query

Filters the WordPress database query used to retrieve users, allowing modification before execution.

add_filter( 'automator_get_users_query', $callback, 10, 1 );

Description

Filters the SQL query used to retrieve WordPress users. Developers can modify the query to customize which users are fetched, add custom conditions, or change the order and limit. The `$wpdb` object is available for building the query.


Usage

add_filter( 'automator_get_users_query', 'your_function_name', 10, 1 );

Parameters

$wpdb (mixed)
This parameter provides access to the WordPress database object, allowing for direct database queries.

Return Value

The filtered value.


Examples

/**
 * Example of filtering the 'automator_get_users_query' hook to add a condition.
 * This example would fetch only users with a specific role (e.g., 'editor').
 */
add_filter(
	'automator_get_users_query',
	'automator_filter_users_by_role',
	10,
	1
);

/**
 * Filters the user query to include only editors.
 *
 * @param string $query The original SQL query string.
 *
 * @return string The modified SQL query string.
 */
function automator_filter_users_by_role( $query ) {
	global $wpdb;

	// Check if the query already has a WHERE clause.
	if ( strpos( $query, 'WHERE' ) === false ) {
		$query .= "
			WHERE EXISTS (
				SELECT 1
				FROM {$wpdb->usermeta} um
				WHERE um.user_id = {$wpdb->users}.ID
				AND um.meta_key = '{$wpdb->prefix}capabilities'
				AND um.meta_value LIKE '%"editor"%'
			)
		";
	} else {
		// If a WHERE clause already exists, we need to append to it correctly.
		// This is a simplified example; a more robust solution might parse the query.
		$query = str_replace(
			'FROM ' . $wpdb->users,
			"FROM {$wpdb->users}
			JOIN {$wpdb->usermeta} um ON {$wpdb->users}.ID = um.user_id
			WHERE um.meta_key = '{$wpdb->prefix}capabilities'
			AND um.meta_value LIKE '%"editor"%'
			AND", // Append the original WHERE clause here if it existed.
			$query
		);
	}

	return $query;
}

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/core/lib/helpers/class-automator-recipe-helpers.php:993

public function wp_users( $limit = 99999 ) {
		global $wpdb;
		// prepare transient key.
		$transient_key = 'automator_transient_users';
		// attempt fetching options from transient.
		$users = Automator()->cache->get( 'uap_transient_users' );
		if ( empty( $users ) ) {
			$query = apply_filters(
				'automator_get_users_query',
				"SELECT ID, display_name
					FROM $wpdb->users
					ORDER BY display_name
					LIMIT 0, $limit"
			);
			$users = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared

			// save fetched posts in a transient for 3 minutes for performance gains.
			$expiration_time = apply_filters( 'automator_get_users_expiry_time', Automator()->cache->expires, $users );
			Automator()->cache->set( $transient_key, $users, 'automator', $expiration_time );
		}

		return apply_filters( 'automator_modify_user_results', $users );
	}


Scroll to Top