• Resolved Marty

    (@bozzmedia)


    Hello,

    Great plugin, thank you. I was curious if there is any current way to exclude a category or categories of Post Grid or Post Masonry? Ideally I could select ‘all’ categories and then exclude a category or two.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Support Team Brainstorm Force

    (@brainstormteam)

    Hello @bozzmedia ,

    To exclude all posts from a specific category. Please use below custom code –

    function filter_post_query( $query_args, $attributes) {
        // Unique class name added from Advanced tab for Post Grid.
        if ( 'my-post-grid-class' == $attributes['className'] ) {
    	// 1 is the category id you want to exclude.
            $query_args['category__not_in'] = array( 1 );
        }
        return $query_args;
    }
    add_filter( 'uagb_post_query_args_grid', 'filter_post_query', 10, 2 );

    See here how to get category ID from here.

    For the Post Masonry block:

    Similarly, just like the above step, you have to first add unique classes to each of the blocks from the Advanced Tab of Post Masonry Block.

    After that, you can use the following code:

    function filter_post_query( $query_args, $attributes) {
        // Unique class name added from Advanced tab for Post Masonry.
        if ( 'my-post-masonry-class' == $attributes['className'] ) {
    	// 1 is the category id you want to exclude.
            $query_args['category__not_in'] = array( 1 );
        }
        return $query_args;
    }
    add_filter( 'uagb_post_query_args_grid', 'filter_post_query', 10, 2 );

    I hope that helps.

    Regards,
    Sweta

    Thread Starter Marty

    (@bozzmedia)

    Brilliant, thank you for the quick and thorough response!

    Plugin Support Team Brainstorm Force

    (@brainstormteam)

    You’re most welcome!
    ?
    If you need anything else we can help you with, don’t hesitate to open a new ticket.
    ?
    Have a nice day!

    Hmm, what do I do wrong? I tried adding it in functions.php in my child theme, tried adding it via the Code Snippet plugin, both not working.

    I am using a custom post type (added via toolset), in GeneratePress theme.

    • This reply was modified 3 years, 9 months ago by floscnos.
    Plugin Support Team Brainstorm Force

    (@brainstormteam)

    Hello @floscnos ,

    It seems like you have opened a ticket with the same issue and our team has already responded to that ticket.

    Could you please check it once?

    This isn’t working for me. I added the function on my functions.php page and then added unique class name for my-post-grid. I then put in the advanced field on my block, but the post from the excluded category is still showing up. Has something changed in the UAG code? Thanks!

    • This reply was modified 3 years, 4 months ago by backinblack.

    Hello @backinblack ,

    Can you please try with the following code once?

    function filter_post_query( $query_args, $attributes) {
     // Unique class name added from Advanced tab for Post Grid.
     if ( 'my-post-grid-class' == $attributes['className'] ) {
     // 123 and 456 is the post id you want to exclude.
     $query_args['post__not_in'] = array( 123,456 );
     }
     return $query_args;
    }
    add_filter( 'uagb_post_query_args_grid', 'filter_post_query', 10, 2 );

    I hope that helps. Let me know how it goes.

    Regards,
    Sweta

    That works great! Thank you so much for your help! You’re a life saver.

    • This reply was modified 3 years, 4 months ago by backinblack.
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Exclude categories from Post Masonry / Post Grid’ is closed to new replies.