Filter uncanny-automator

automator_allowed_category_taxonomy_children_taxonomies

Filters allowed category taxonomy children taxonomies to control which taxonomies can have subcategories.

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

Description

Fires to define which taxonomies are allowed for fetching category children. Developers can filter this array to include or exclude specific taxonomies from being considered when retrieving hierarchical term data for automations. Defaults to 'category'.


Usage

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

Return Value

The filtered value.


Examples

<?php
/**
 * Example: Allow 'post_tag' taxonomy in addition to 'category' for 'automator_allowed_category_taxonomy_children_taxonomies' filter.
 *
 * This filter allows developers to specify which taxonomies are eligible for
 * retrieving child terms in the Uncanny Automator plugin. By default, it only
 * allows 'category'. This example demonstrates how to add 'post_tag' to the
 * list of allowed taxonomies.
 *
 * @param array $allowed_taxonomies An array of taxonomy slugs that are allowed.
 * @return array The modified array of allowed taxonomy slugs.
 */
function my_automator_allow_post_tags_for_child_taxonomies( $allowed_taxonomies ) {
    // Add 'post_tag' to the list of allowed taxonomies if it's not already there.
    if ( ! in_array( 'post_tag', $allowed_taxonomies, true ) ) {
        $allowed_taxonomies[] = 'post_tag';
    }

    // Optionally, you could also remove 'category' if you only wanted to allow 'post_tag'.
    // $allowed_taxonomies = array_diff( $allowed_taxonomies, array( 'category' ) );

    return $allowed_taxonomies;
}
add_filter( 'automator_allowed_category_taxonomy_children_taxonomies', 'my_automator_allow_post_tags_for_child_taxonomies', 10, 1 );

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/integrations/wp/helpers/wp-helpers.php:893
uncanny-automator-pro/src/integrations/wp/helpers/wp-pro-helpers.php:998

public function get_term_child_of( $post_terms, $parent_term_id, $taxonomy, $post_id ) {

		if ( empty( $post_terms ) || ! is_array( $post_terms ) ) {
			return false;
		}

		// Check Post Type for the post
		$allowed_post_types = apply_filters( 'automator_allowed_category_taxonomy_children_post_types', array( 'post' ) );
		if ( ! in_array( get_post_type( $post_id ), $allowed_post_types, true ) ) {
			return false;
		}

		$allowed_taxonomies = apply_filters( 'automator_allowed_category_taxonomy_children_taxonomies', array( 'category' ) );
		if ( ! is_array( $allowed_taxonomies ) || ! in_array( $taxonomy, $allowed_taxonomies, true ) ) {
			return false;
		}

		// Get all child terms of the parent term
		$child_terms = get_term_children( $parent_term_id, $taxonomy );
		if ( empty( $child_terms ) || is_wp_error( $child_terms ) ) {
			return false;
		}

		foreach ( $post_terms as $post_term ) {
			if ( in_array( $post_term->term_id, $child_terms, true ) && $post_term->taxonomy === $taxonomy ) {
				return $post_term;
			}
		}

		return false;
	}


Scroll to Top