Filter uncanny-automator

automator_wp_query_args

Filters the WP_Query arguments before executing a query, allowing modification of the query parameters.

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

Description

This filter hook allows developers to modify the arguments used for WordPress queries within the Automator plugin. It fires after the initial query arguments are prepared and before they are used. Developers can use this hook to customize the query, for example, to exclude specific posts, change the order, or add custom parameters.


Usage

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

Parameters

$args (mixed)

Return Value

The filtered value.


Examples

/**
 * Example of how to use the 'automator_wp_query_args' filter hook to modify
 * the arguments passed to a WordPress query within the Automator plugin.
 *
 * This example demonstrates how to add a specific post type to the query
 * and also how to exclude posts from a particular category.
 *
 * @param array $args The original query arguments.
 * @return array The modified query arguments.
 */
add_filter( 'automator_wp_query_args', function( $args ) {

    // Ensure $args is an array before proceeding.
    if ( ! is_array( $args ) ) {
        return $args;
    }

    // Let's say we want to specifically query for 'post' and 'custom_post_type'.
    // If 'post_type' is not already set or is empty, we'll add our desired types.
    if ( ! isset( $args['post_type'] ) || empty( $args['post_type'] ) ) {
        $args['post_type'] = array( 'post', 'custom_post_type' );
    }
    // If 'post_type' is set but doesn't include 'post', we might want to add it.
    elseif ( is_array( $args['post_type'] ) && ! in_array( 'post', $args['post_type'] ) ) {
        $args['post_type'][] = 'post';
    }
    // If it's a string, convert it to an array and add 'post'.
    elseif ( is_string( $args['post_type'] ) && $args['post_type'] !== 'post' ) {
        $args['post_type'] = array( $args['post_type'], 'post' );
    }

    // Example: Exclude posts from a category with ID 10.
    // If 'category__not_in' is not set, initialize it.
    if ( ! isset( $args['category__not_in'] ) || empty( $args['category__not_in'] ) ) {
        $args['category__not_in'] = array( 10 );
    }
    // If 'category__not_in' is set but doesn't include 10, add it.
    elseif ( is_array( $args['category__not_in'] ) && ! in_array( 10, $args['category__not_in'] ) ) {
        $args['category__not_in'][] = 10;
    }

    // You can add more complex logic here, like checking other conditions
    // or dynamically determining what to include/exclude based on other factors.

    return $args;

}, 10, 1 ); // Priority 10, accepts 1 argument

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:819

public function wp_query( $args, $add_any_option = false, $add_any_option_label = null, $is_all_label = false ) {

		// Allow automator to load this wp_query results from MCP requests. Backwards compatibility.
		$is_mcp = ( defined( 'REST_REQUEST' ) && REST_REQUEST )
			&& isset( $_SERVER['REQUEST_URI'] ) && false !== strpos( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), '/automator/v1/mcp' );

		if ( ! $this->load_helpers && ! $is_mcp ) {
			return array();
		}

		if ( empty( $args ) ) {
			return array();
		}

		/** @var array $args Allow developers to modify $args. */
		$args = apply_filters( 'automator_wp_query_args', $args );

		// Translate legacy positional params into modern array params.
		$params = $args;

		if ( $add_any_option ) {
			if ( $is_all_label ) {
				$params['include_all'] = true;
				$params['all_label']   = $this->resolve_any_option_label( $add_any_option_label, true );
			} else {
				$params['include_any'] = true;
				$params['any_label']   = $this->resolve_any_option_label( $add_any_option_label, false );
			}
		}

		$options = automator_wp_query( $params, 'legacy' );

		return apply_filters( 'automator_modify_option_results', $options, $args );
	}


Scroll to Top