failed to filter hook `get_terms_defaults`

I’ve failed to filter hook get_terms_defaults, I got this notices and it won’t to load any term

Notice: Undefined index: number in
/var/www/html/wp-merwaa/wp-includes/taxonomy.php on line 1123

Notice: Undefined index: offset in
/var/www/html/wp-merwaa/wp-includes/taxonomy.php on line 1124

Notice: Undefined index: parent in
/var/www/html/wp-merwaa/wp-includes/taxonomy.php on line 1140

Notice: Undefined index: get in
/var/www/html/wp-merwaa/wp-includes/taxonomy.php on line 1144

Notice: Undefined index: child_of in
/var/www/html/wp-merwaa/wp-includes/taxonomy.php on line 1163

Notice: Undefined index: parent in
/var/www/html/wp-merwaa/wp-includes/taxonomy.php on line 1164

Notice: Undefined index: orderby in
/var/www/html/wp-merwaa/wp-includes/taxonomy.php on line 1189

Notice: Undefined index: order in
/var/www/html/wp-merwaa/wp-includes/taxonomy.php on line 1222

Notice: Undefined index: exclude in
/var/www/html/wp-merwaa/wp-includes/taxonomy.php on line 1235

Notice: Undefined index: exclude_tree in
/var/www/html/wp-merwaa/wp-includes/taxonomy.php on line 1236

Notice: Undefined index: include in
/var/www/html/wp-merwaa/wp-includes/taxonomy.php on line 1237

Notice: Undefined index: childless in
/var/www/html/wp-merwaa/wp-includes/taxonomy.php on line 1269

My code

public function exclude_not_allowed_terms() {
  if(!current_user_can('edit_others_posts')) {
    $args['meta_query'] = array(
      'key'       => 'owner',
      'value'     => get_current_user_id()
  );
}
add_filter('get_terms_defaults','exclude_not_allowed_terms');

1 Answer
1

Filters are for modifying data that’s passed through them. You need to take the input, modify it, then return it.

function exclude_not_allowed_terms( $args, $taxonomies ) {
    if(!current_user_can('edit_others_posts')
      && in_array( 'my-taxonomy', $taxonomies ) ) {
        $args['meta_query'] = array(
            array(
                'key'    => 'owner',
                'value'  => get_current_user_id()
            )
        );
    }
    return $args;
}
add_filter( 'get_terms_defaults','exclude_not_allowed_terms', 20, 2 );

Leave a Comment