Child terms from multiple parents?

We can filter the generated SQL query through the terms_clauses filter before the SQL query is executed. What we will be doing is to introduce a new parameter called wpse_parents which will accept an array of parent term ID’s to get children from. Note, this new parameter works exactly the same as the build-in parameter, parent, as it will return only direct children of the parent.

THE FILTER

The code is commented for easy understanding and basic description of what a particular piece of code is doing. Note, all code requires at least PHP 5.4

add_filter( 'terms_clauses', function ( $pieces, $taxonomies, $args )
{
    // Bail if we are not currently handling our specified taxonomy
    if ( !in_array( 'speight_plans', $taxonomies ) )
        return $pieces;

    // Check if our custom argument, 'wpse_parents' is set, if not, bail
    if (    !isset ( $args['wpse_parents'] )
         || !is_array( $args['wpse_parents'] )
    ) 
        return $pieces;

    // If  'wpse_parents' is set, make sure that 'parent' and 'child_of' is not set
    if (    $args['parent']
         || $args['child_of']
    )
        return $pieces;

    // Validate the array as an array of integers
    $parents = array_map( 'intval', $args['wpse_parents'] );

    // Loop through $parents and set the WHERE clause accordingly
    $where = [];
    foreach ( $parents as $parent ) {
        // Make sure $parent is not 0, if so, skip and continue
        if ( 0 === $parent )
            continue;

        $where[] = " tt.parent="$parent"";
    }

    if ( !$where )
        return $pieces;

    $where_string = implode( ' OR ', $where );
    $pieces['where'] .= " AND ( $where_string ) ";

    return $pieces;
}, 10, 3 );

Just a note, the filter is build in such a way that it does not allow the parent and child_of parameters being set. If any of those parameters are set, the filter bails early and is not applied

BASIC USAGE

If you need to query child terms from terms 15 and 16, you can run the following query to achieve your goal

$args = [
    'wpse_parents' => [15, 16]
];
$terms = get_terms( 'speight_plans', $args );

Leave a Comment