Skip Navigation

[Gelöst] How to exclude one taxonomy from all but its own archive

This support ticket is created vor 6 Jahre, 8 Monate. 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.

This topic contains 2 Antworten, has 2 Stimmen.

Last updated by marcS-7 vor 6 Jahre, 8 Monate.

Author
Artikel
#397025

One of the taxonomies of my properties is property-status, and one of these statuses is "sold". I would like properties with this taxonomy to be exluded from all search results and archives throughout the site, EXCEPT on their own archive page with all of the sold properties together. How can I do this? Maybe it has to be done somehow in functions.php, as I know I can't have two taxonomy queries in views. I have also tried putting a conditional statement in the loop-item-grid view, so that only displays properties that do not have that taxonomy, but the columns are set outside of that view, and at times, outside of the loop, so this sometimes leaves gaps. Thank you!

#397028

Typical! Spend hours trying to figure this out and then find the solution just after posting the question here. Adding this to functions.php did the trick, should anyone else want to do the same thing:

function exclude_category( $query ) {
    if ( $query->is_tax() && $query->is_main_query() && !$query->is_tax('property-status','sold')) {

        $args = array(
            'post_type' => 'house',
            'tax_query' => array(
                array(
                    'taxonomy' => 'property-status',
                    'field'    => 'slug',
                    'terms'    => array( 'sold' ),
                    'operator' => 'NOT IN',
                )
            ),
        );

        $query->set( 'tax_query', $args);

    }
}
add_action( 'pre_get_posts', 'exclude_category' );
#489364

I just wanted to say a quick thanks, me too I was looking to solve the same use case.