Skip Navigation

[Resolved] Filter display of custom post Taxonomy

This support ticket is created 2 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 23 replies, has 2 voices.

Last updated by jonathanR-5 2 years, 9 months ago.

Assisted by: Jamal.

Author
Posts
#2110285

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello, please check my reply here https://toolset.com/forums/topic/filter-display-of-custom-post-taxonomy/#post-2109993

I needed your approval to take a copy of your website and debug it locally.

I checked again the view and the custom code seems to work as expected. I would say, it was just a cache issue during my tests this morning. And now the cache is invalidated and the view does not display the "Ladder Faculty" term anymore. Check this screenshot hidden link

The final code is then:

add_filter('wp_get_object_terms', 'exclude_taxonomy_term', 10, 4);
function exclude_taxonomy_term($terms, $object_ids, $taxonomies, $args) {
    global $WP_Views;
     
    $taxonomy = "faculty-fields";
    $term_slug = "ladder-faculty";
    $view_id = 1723;
     
    if ( $WP_Views->current_view == $view_id ) {
        foreach( $terms as $i => $term ) {
            //echo $view_id;
            //echo $terms[$i];
            // print_r($terms);
            if ( $term->slug == $term_slug && $term->taxonomy == $taxonomy ) {
                unset( $terms[$i] );
            }
        }
    }
     
    return $terms;
}

Let me know if there is something else I can help with.

#2110393

Yes, please go ahead and take a copy of the site to debug it locally. Thanks.

#2110395

Also, please let me know how to add additional terms to not be displayed:
$term_slug = "ladder-faculty" ..... ;

#2110791

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

To exclude multiple terms, you can update the code at lines 7 and 15 like this:

add_filter('wp_get_object_terms', 'exclude_taxonomy_term', 10, 4);
function exclude_taxonomy_term($terms, $object_ids, $taxonomies, $args) {
    global $WP_Views;
      
    $taxonomy = "faculty-fields";
    $term_slugs = array("ladder-faculty", "another-term", "third-term");
    $view_id = 1723;
      
    if ( $WP_Views->current_view == $view_id ) {
        foreach( $terms as $i => $term ) {
            //echo $view_id;
            //echo $terms[$i];
            // print_r($terms);
            if ( in_array($term->slug, $term_slugs ) && $term->taxonomy == $taxonomy ) {
                unset( $terms[$i] );
            }
        }
    }
      
    return $terms;
}

Notice how I renamed $term_slug into $term_slugs(with an s).

#2112245

Thank you! The code seems to be working now. Can you please tell me what the issue was before and why the code was not running on our end when we tested? I'd like to document this.

#2112309

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

I forgot the following line of code in the first snippet I have shared:

add_filter('wp_get_object_terms', 'exclude_taxonomy_term', 10, 4);

Without this line of code, nothing will be executed. You can read more about hooks here https://developer.wordpress.org/plugins/hooks/

#2112475

Thanks. Sorry but I'm a bit confused because in your previous message you stated that that line of code did not solve the issue: "Unfortunately, that did not seem to be the only issue. The code does not exclude the term from the results. I need to debug this in my local development environment."
Can you please tell me why you needed to run it locally ?

#2113205

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

You are right. But, it was only a cache issue. First, I added the line of code and my tests failed, and I asked for taking a copy of your website to analyze it. Then, it worked. So, my suspicion is that cache was the issue. Check this reply https://toolset.com/forums/topic/filter-display-of-custom-post-taxonomy/page/2/#post-2110285

#2119447

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.