Filter uncanny-automator-pro

automator_pro_db_query_select_query_run_action_statement

Filters the SELECT query statement when running a database query action.

add_filter( 'automator_pro_db_query_select_query_run_action_statement', $callback, 10, 2 );

Description

Allows modification of the SQL SELECT statement generated by the DB Query action before it's executed. Developers can alter the query string or the arguments passed to `wpdb->prepare` to customize data retrieval, like adding custom joins or clauses. This hook is ideal for advanced DB Query integrations.


Usage

add_filter( 'automator_pro_db_query_select_query_run_action_statement', 'your_function_name', 10, 2 );

Parameters

$stmt (mixed)
This parameter contains the SQL SELECT statement being built for the database query.
$args (mixed)
This parameter contains the SQL SELECT statement that will be executed against the database.

Return Value

The filtered value.


Examples

<?php

/**
 * Example of modifying the SQL SELECT statement before it's prepared for execution.
 *
 * This filter can be used to dynamically add or modify parts of the SELECT query,
 * such as adding specific columns, modifying WHERE clauses, or altering the ORDER BY.
 *
 * @param string $stmt The SQL SELECT statement string.
 * @param array  $args An array of arguments used to construct the query, potentially including:
 *                     'table', 'where', 'limit', 'order_by', 'order'.
 *
 * @return string The modified SQL SELECT statement string.
 */
add_filter(
	'automator_pro_db_query_select_query_run_action_statement',
	function ( $stmt, $args ) {
		// Example: If a specific table is being queried, and we want to ensure
		// a particular column is always selected, or modify the WHERE clause.

		// Let's assume we want to always select the 'ID' column from the posts table
		// and add a condition to ensure the post status is 'publish'.
		if ( isset( $args['table'] ) && $args['table'] === 'posts' ) {
			// This is a simplified example. In a real-world scenario, you'd need to be
			// very careful about sanitizing and ensuring the resulting SQL is valid.
			// You might parse the existing $stmt and carefully inject your changes.

			// A more robust approach would involve parsing the existing $stmt
			// or reconstructing it with the new conditions.
			// For demonstration, let's prepend 'ID, ' if it's not already there.
			if ( strpos( $stmt, 'SELECT ID,' ) === false && strpos( $stmt, 'SELECT *' ) !== false ) {
				$stmt = str_replace( 'SELECT *', 'SELECT ID, *', $stmt );
			}

			// Safely add a WHERE clause condition if it doesn't exist.
			// This is highly dependent on the structure of $stmt.
			// A more reliable way might be to rebuild the query from $args.
			if ( ! isset( $args['where'] ) || strpos( $args['where'], 'post_status' ) === false ) {
				$new_where = 'post_status = 'publish'';
				if ( ! empty( $args['where'] ) ) {
					// Append with AND. Assumes $args['where'] is already a valid WHERE clause part.
					$new_where = $args['where'] . ' AND ' . $new_where;
				}
				$args['where'] = $new_where; // Update $args so subsequent logic might use it.

				// Reconstruct the statement with the new WHERE clause.
				// This is safer than trying to parse and inject into $stmt directly.
				// Assuming $args contains 'table', 'where', 'limit', 'order_by', 'order'
				global $wpdb;
				$where_sql = '';
				if ( ! empty( $args['where'] ) ) {
					// This is where you'd ideally use prepare() with the conditions in $args['where'].
					// For simplicity, assuming $args['where'] is already safe or built with prepare().
					$where_sql = ' WHERE ' . $args['where'];
				}

				$order_by_sql = '';
				if ( ! empty( $args['order_by'] ) ) {
					$order_by_sql = ' ORDER BY ' . esc_sql( $args['order_by'] );
					if ( ! empty( $args['order'] ) ) {
						$order_by_sql .= ' ' . ( strtoupper( $args['order'] ) === 'DESC' ? 'DESC' : 'ASC' );
					}
				}

				$limit_sql = '';
				if ( ! empty( $args['limit'] ) ) {
					$limit_sql = ' LIMIT ' . absint( $args['limit'] );
				}

				// Rebuild the statement.
				$stmt = sprintf(
					'SELECT %s FROM %s%s%s%s',
					( strpos( $stmt, 'SELECT *' ) !== false ? '*' : 'ID, *' ), // Example of handling selected columns
					esc_sql( $args['table'] ),
					$where_sql,
					$order_by_sql,
					$limit_sql
				);
			}
		}

		// Always return the modified statement.
		return $stmt;
	},
	10, // Priority: 10 is the default priority
	2   // Accepted arguments: $stmt and $args
);

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

uncanny-automator-pro/src/integrations/db-query/actions/select-query-run.php:317

'table'     => $table,
			'where'     => $where,
			'limit'     => $limit,
			'order_by'  => $order_by,
			'order'     => $order_safe,
		);

		$stmt = apply_filters( 'automator_pro_db_query_select_query_run_action_statement', $stmt, $args );

		// Ignoring PHPCS since we're constructing our own safe strings.
		$query = $wpdb->prepare( $stmt, absint( $limit ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared

		if ( empty( $query ) ) {
			throw new Exception( sprintf( 'Invalid query provided. The generated query is: %s', esc_html( $stmt ) ) );
		}

Scroll to Top