Sauter la navigation

[Résolu] Remove Specific Taxonomy Terms from View

This support ticket is created Il y a 2 semaines. 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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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: Asia/Kolkata (GMT+05:30)

Ce sujet contient 0 réponses, a 1 voix.

Dernière mise à jour par zacharyL Il y a 1 semaine.

Assisté par: Minesh.

Auteur
Publications
#2787492

I'm trying to remove or filter out a specific taxonomy term from a taxonomy view. There was a support thread about this exact request but the solution provided doesn't appear to be working for me (https://toolset.com/forums/topic/hiding-taxonomy-terms-from-views).

I swear I was able to get this to work in the past, but now I don't know what's going on and why it's not working for me now. Anyway, I have a custom taxonomy called "Departments" and there's one particular term (Owner Operators) that I would like to exclude from my landing page View (seen here: lien caché). As you can see from my link, the section exists on this page when it shouldn't. Here's the code I was utilizing from the previous support thread that was trying to address this issue:

// Remove Owner Operators from Department Views Output
add_filter( 'wpv_filter_query', 'exclude_terms_func', 10, 3 );
function exclude_terms_func($query, $setting, $views_ID)
{
    $my_views = array(2617);
    if( in_array( $views_ID, $my_views ) ) // your view id
    {
        $query['tax_query'][] = array(
            'taxonomy' => 'department', // taxonomy name
            'field' => 'slug',
            'terms' => array( 'owner-operators' ), // term slug for exclude
            'operator' => 'NOT IN'
        );
        $query['tax_query']['relation'] = 'AND';
    }
    return $query;
}

I suppose I could use CSS to hide the section, but since each View loop output pulls a couple other Views related to the taxonomy terms, I'd like to reduce how many of those show up on the page if it's possible.

Any help in this matter would be greatly appreciated.

#2787530

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I'll have to check how exactly you configure the display of the taxonomy.

Can I've admin access details and let me review your current settings and then I will be able to guide you in the right direction.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2787841

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

I see you created newsted views where parent view is taxonomy view and child view is post view.

You have top/parent view set to loop through the taxonomy terms:
=> lien caché

To the above view, I've added the Query Fitler for taxonomy term as follows:
=> lien caché

 
Taxonomy term filter
Taxonomy is One of these: Company Drivers, Fleet Maintenance, Office Jobs, Operations

Please check the following screenshot: lien caché

Can you please confirm it works as expected now:
- lien caché

#2787929
Screenshot 2024-12-12 at 11-29-21 Edit View ‹ Barnhart Inc. — WordPress.png

I'm aware of this implementation, but I didn't want to use it as it's too specific. If a new term is added, it doesn't get automatically selected for inclusion (see attached screenshot where the term "New Department" is not selected) and in fact defaults to deselected. I need to configure EXCLUSIONS not INCLUSIONS, so that if the client wants to add a new Department, they can freely do so without having to modify the View configuration.

With post views, you can set exclusions in the query filters by post ID, but that is not an option with the taxonomy views, which is why I was trying to use that code in that support thread I had originally linked to.

I suppose I could add in a conditional argument within the View output, to filter out the term(s) I don't want to show on the Careers landing page, but that's such a dirty way of handling these kinds of things. Can you review the code provided by the support rep in the support thread I was referring to in my original post, please, and see why it wouldn't be working for my application?

Just so you know, I deleted that query filter you added as it's not what I'm looking to implement in this application.

#2788019

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Please check now: lien caché

In that case, I've added the following view's filter to "Custom Code" section offered by Toolset:
=> lien caché

add_filter( 'wpv_filter_taxonomy_query', 'func_allow_specific_terms_taxonomy_view', 10, 3 );
function func_allow_specific_terms_taxonomy_view( $tax_query_settings, $view_settings, $view_id ) {
  
    if($view_id == 2617) {
      //// add all term slugs that you want to allow by default 
      $allowed_terms = array('company-drivers','fleet-maintenance','office-jobs','operations');
      $tax_query_settings['slug'] = $allowed_terms;
    }
    return $tax_query_settings;
}

More info:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_taxonomy_query

#2788111

Okay, but can we set EXCLUSIONS not inclusions?

Your custom code is establishing a list of inclusions, which is why I didn't want to use that feature in the Query Filters selections in the first place.

$allowed_terms = array('company-drivers','fleet-maintenance','office-jobs','operations');

This change just makes adding more Departments even more inaccessible to the client.

I want to be able to exclude the term "Owner Operators", with the option of adding more exclusions, ideally with an array, if I needed to.

I moved your code snippet to the child theme's functions.php to line 311 for your reference.

#2788476

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Ok for excludsitons - you will have to add the term ID.

I've adjusted the code as given under to your child therm's funcitons.php file:

add_filter( 'wpv_filter_taxonomy_query', 'func_allow_specific_terms_taxonomy_view', 10, 3 );
function func_allow_specific_terms_taxonomy_view( $tax_query_settings, $view_settings, $view_id ) {

    if($view_id == 2617) {
        //  exclue term ids 
       $tax_query_settings['exclude'] = array(49,999,888);

       
    }
    return $tax_query_settings;
}

- you can adjust the array value for exclutions and add more as required and replace 999 and 888 with your original term ids

Please check the following related ticket that may help you:
- https://toolset.com/forums/topic/exclude-several-taxonomies-from-a-view/

#2788751

Oh, excellent, thank you. This code snippet appears to work marvelously. Once again, I appreciate the help.