Filter uncanny-automator

automator_api_register_loop_filters

Allow additional loop filters to be registered. Filters to register additional loop filters with the API.

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

Description

Fires during API loop filter registration. Developers can add custom loop filters by returning an associative array of filter codes and their corresponding filter objects. These will then be available for use in API loop queries.


Usage

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

Parameters

$filters (array)
Existing filters array.

Return Value

The filtered value.


Examples

/**
 * Add a custom loop filter to filter posts by a specific custom field value.
 *
 * This function registers a new loop filter that allows users to select posts
 * based on the value of a custom field named 'featured_post'.
 *
 * @param array $existing_filters The array of currently registered loop filters.
 * @return array The modified array of loop filters, including the new custom filter.
 */
add_filter( 'automator_api_register_loop_filters', 'my_custom_automator_featured_post_filter', 10, 1 );
function my_custom_automator_featured_post_filter( $existing_filters ) {
    // Define our new custom filter.
    $custom_filter = array(
        'label'      => __( 'Featured Posts', 'your-text-domain' ),
        'type'       => 'select', // Or 'checkbox', 'radio', depending on desired UI.
        'options'    => array(
            'yes' => __( 'Yes', 'your-text-domain' ),
            'no'  => __( 'No', 'your-text-domain' ),
        ),
        'meta_key'   => 'featured_post', // The custom field key to check.
        'description' => __( 'Filter posts that are marked as featured.', 'your-text-domain' ),
    );

    // Add our custom filter to the existing array using a unique code.
    $existing_filters['featured_post_filter'] = $custom_filter;

    return $existing_filters;
}

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/api/components/loop/filter/registry/class-wp-registry.php:269

private function load_filters_from_wordpress(): void {
		/**
		 * Filter to get registered loop filters from Pro.
		 *
		 * Pro structure: $filters[$integration][$meta] = [
		 *     'integration' => 'WP',
		 *     'meta' => 'WP_USER_HAS_ROLE',
		 *     'loop_type' => 'users',
		 *     'sentence' => '...',
		 *     'sentence_readable' => '...',
		 *     'fields' => [...],
		 * ]
		 *
		 * @param array $filters Registered loop filters keyed by integration then meta.
		 */
		$pro_filters = apply_filters( 'automator_integration_loop_filters', array() );

		// Pro returns nested structure: $filters[$integration][$meta] = $filter_data
		foreach ( $pro_filters as $integration_code => $integration_filters ) {
			if ( ! is_array( $integration_filters ) ) {
				continue;
			}

			foreach ( $integration_filters as $meta => $filter ) {
				if ( ! is_array( $filter ) ) {
					continue;
				}

				// Map Pro's 'meta' to domain's 'code'
				$filter['code'] = $filter['meta'] ?? $meta;

				// Filters from Pro hook are always pro features.
				$filter['is_pro'] = true;

				$this->filters[ strtoupper( $filter['code'] ) ] = $this->normalize_filter_definition( $filter );
			}
		}

		/**
		 * Allow additional loop filters to be registered.
		 *
		 * @param array $filters Existing filters array.
		 */
		$additional_filters = apply_filters( 'automator_api_register_loop_filters', array() );

		foreach ( $additional_filters as $code => $filter ) {
			$this->register_filter( $code, $filter );
		}
	}


Scroll to Top