Site logo

Archived topic

Featured category post

17 replies · Started by Vicky on September 9, 2021

Viewing posts 16–18 of 18

However, as you pointed out there are 2 queries on the page. On the green background where all the posts for the category are listed, the problem is that the featured post is showing there too. Is there a way to automatically exclude the featured post from the green background area?

It's not ideal to mess with that as this page is the default archive page Wordpress generates that is meant to list ALL posts that are under category "popular", regardless of what other category or tags they have.

But if you really must do it, you can try going with pre_get_post filter.

Example:

function exclude_with_post_tag_on_cat_archive( $query ) { 
    $category = get_queried_object();
    $cat_id = $category->term_id;	
    $cat_slug = $category->slug;
    $featured_tag = 'featured-' . $cat_slug;
    $tag_id = get_term_by('slug', $featured_tag ,'post_tag');

    if ( $query->is_category($cat_id) && $query->is_main_query() ) {  

        $query->set( 'tag__not_in', array( $tag_id ) ); 

    } 

} 

add_action( 'pre_get_posts', 'exclude_with_post_tag_on_cat_archive' );

Hi Elvin,

Thanks for all your help on this, I appreciate that I'm asking for an unusual solution here!

Unfortunately, that last bit of code didn't work out, the featured post is still showing up in the 2nd query. I think I'll probably have to go back and rethink my design!

Thanks anyway,
Vicky

No problem. :D

This archived topic is closed to new replies.