Filter uncanny-automator

automator_allowed_post_types

Filters the array of allowed post types for the Automator plugin.

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

Description

Filters the array of post type slugs that the plugin considers for data retrieval. Developers can use this hook to exclude or include specific custom post types when fetching automator-related data, ensuring data is only gathered from relevant sources.


Usage

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

Return Value

The filtered value.


Examples

/**
 * Example: Filter to exclude 'automator_closure' post type from allowed types.
 *
 * This function demonstrates how to hook into the 'automator_allowed_post_types'
 * filter to conditionally remove specific post types from the list of allowed
 * types. In this example, we're removing the 'automator_closure' post type.
 *
 * @param array $allowed_post_types An array of post type slugs.
 * @return array The modified array of allowed post types.
 */
add_filter( 'automator_allowed_post_types', 'my_custom_automator_allowed_post_types', 10, 1 );

function my_custom_automator_allowed_post_types( $allowed_post_types ) {
	// Define the post type to be excluded.
	$post_type_to_exclude = AUTOMATOR_POST_TYPE_CLOSURE; // Assuming AUTOMATOR_POST_TYPE_CLOSURE is defined elsewhere, e.g., 'automator_closure'

	// Check if the post type to exclude exists in the current array.
	if ( in_array( $post_type_to_exclude, $allowed_post_types, true ) ) {
		// If it exists, remove it from the array.
		$allowed_post_types = array_diff( $allowed_post_types, array( $post_type_to_exclude ) );
	}

	// Return the modified array of allowed post types.
	return $allowed_post_types;
}

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/utilities/class-automator-get-data.php:1257

public function maybe_get_recipe_id( $id ) {
		if ( is_object( $id ) ) {
			$id = isset( $id->ID ) ? $id->ID : null;
		}

		if ( is_null( $id ) || ! is_numeric( $id ) ) {
			return 0;
		}

		$allowed_post_types = apply_filters(
			'automator_allowed_post_types',
			array(
				AUTOMATOR_POST_TYPE_RECIPE,
				AUTOMATOR_POST_TYPE_TRIGGER,
				AUTOMATOR_POST_TYPE_ACTION,
				AUTOMATOR_POST_TYPE_CLOSURE,
			)
		);

		$post = get_post( $id );

		if ( $post instanceof WP_Post && AUTOMATOR_POST_TYPE_RECIPE === $post->post_type ) {
			return absint( $post->ID );
		}

		if ( $post instanceof WP_Post && in_array( $post->post_type, $allowed_post_types, true ) ) {
			return absint( $post->post_parent );
		}

		return 0;
	}

Scroll to Top