Okay
  Public Ticket #2297047
Filter Portfolio Categories in "slug"-View
Closed

Comments

  •  15
    hahni started the conversation

    Another question about the portfolio:

    Normally I have the "main-slug"-portfolio view and all posted portfolio items are "online" - a nice function and it works great with the possibility of the custom slug (you see it in action on my page).

    Is it possible to filter the categories for this "main" view? I want to hide one category for online view, but I want to see some items of this category in another "special" view online.

    Should I do this with "pre_get_posts"? I tried this some month ago and the problem was, that the "filter" in the head of the portfolio view sometimes showed me the "hidden" category.

    I hope I wrote understandable.


    Regards André


  • [deleted] replied

    Hi hahni,

    You can apply custom category filtering for portfolio archive with this code:

    add_filter( 'kalium_portfolio_pre_query', function ( $query_args ) {
        if ( ! isset( $query_args['tax_query'] ) ) {
            $query_args['tax_query'] = [];
        }
        $query_args['tax_query'][] = [
            'relation' => 'OR',
            [
                'taxonomy'         => 'portfolio_category',
                'field'            => 'slug',
                'operator'         => 'NOT IN',
                'terms'            => 'my-category-slug',
                'include_children' => false,
            ],
        ];
        return $query_args;
    } );
    

    So what exactly this does only excludes portfolio items which have "my-category-slug". However if an item has two categories assigned: "my-category-slug" and "my-another-category-slug" and you want to show items that have "my-another-category-slug" regardless they have the "excluding" category, then you have to do include all other categories in another query parameter, here is how:

    add_filter( 'kalium_portfolio_pre_query', function ( $query_args ) {
        if ( ! isset( $query_args['tax_query'] ) ) {
            $query_args['tax_query'] = [];
        }
        $query_args['tax_query'][] = [
            'relation' => 'OR',
            [
                'taxonomy'         => 'portfolio_category',
                'field'            => 'slug',
                'operator'         => 'NOT IN',
                'terms'            => 'my-category-slug',
                'include_children' => false,
            ],
            [
                'taxonomy'         => 'portfolio_category',
                'field'            => 'slug',
                'operator'         => 'IN',
                'terms'            => [ 'my-another-category-slug', 'some-other-category', 'till-the-end' ],
                'include_children' => false,
            ],
        ];
        return $query_args;
    } );
    

    I hope this will help you solve your issue.

  •  15
    hahni replied

    Hello Arlind


    thanks for your answer but its not the right way:

    After including the code snippet and adding a new portfolio item in the ""excluded"" category I tested and it works perfect. BUT:

    in admin i coud choose the new added ""excluded"" portfolio item in the special internal usage page but if I open the page the added item is not included - I think that your filter works now for all pages and not only for the portfolio archive page. if you want you could test it on my page.

  •  15
    hahni replied

    Good Morning Arlind

    I extended your script with an additional if-condition:

    if ( is_post_type_archive ( 'portfolio' ) ) {

    YOUR CODE HERE WITHOUT THE return

     }

    return $query_args;


    and now it works perfect - I have now the filtered portfolio items in the main web view and the possibility to show "the hidden portfolio" an other pages for internal use or prrsentations.


    Thanks a lot