"Index > IS > Last" not working on filtered data

Hi @alexander

I figured this would be more suitable to mention in the Beta forum.

I have a Looper with the list of terms, and there are slashes between them with conditions to hide the last one.

That stops working as soon as I narrow down the list of the terms. I have few conditions in the provider with {{dc:term:slug}} > IS > term-slug because I cannot list all the available terms.

The last slash is showing in this case despite of the Index > IS > Last condition.

Thanks!

Hi @Misho,

I see what you mean. Element Conditions just say “Hide this element” but don’t change the actual Looper Provider data. When you retrieve the Looper index, it doesn’t have any awareness of what you did with the previous items, so it will only be considered “Last” on the final element of the Looper.

I’ll keep this use case in mind, but I can’t think of a way at the moment to remedy this without introducing other issues.

Which terms provider are you using? Meanwhile, I might be able to get you a custom PHP function to do the filtering on the backend so the data coming through the Provider already excludes them in advance.

1 Like

Hi @alexander!

No Sweat, I got a nice design without the dividers. :slight_smile: Just wanted to point out the use case.

image

It was the All terms > Product categories provider.

Thanks!

Ok thanks! So this custom code will setup a filtered term looper

add_filter('cs_looper_custom_filtered_terms', function( $result, $args ) {
  
  $terms = get_terms( [
    'taxonomy'   => isset( $args['taxonomy'] ) ? $args['taxonomy'] : 'category',
    'hide_empty' => isset($args['hide_empty'] ) ? $args['hide_empty'] : false,
  ] );

  $exclude = isset( $args['exclude'] ) && is_array( $args['exclude'] ) ? $args['exclude'] : [];

  if ( is_array( $terms ) ) {
    return array_filter( $terms, function( $term ) use ( $exclude ) {
      return ! in_array( $term->slug, $exclude );
    });
  }

  return $terms;  

}, 10, 2 );

Set Looper Provider Type to filtered_terms and setup the params like this:

{
  "taxonomy": "category",
  "exclude": ["red", "blue"]
}

It will return all categories but omit terms with slug “red” or “blue”.

1 Like

Alexander, thanks a lot! I will definitely test this! :+1:

You’re welcome!