[Support request] Limit display rule to Category AND Tag Together

Home Forums Support [Support request] Limit display rule to Category AND Tag Together

Home Forums Support Limit display rule to Category AND Tag Together

Viewing 10 posts - 31 through 40 (of 40 total)
  • Author
    Posts
  • #1058843
    Heather

    Thanks!
    Unfortunately, now all post types are appearing on both the blog archive page (/blog), and the category archive pages. But, this did have all the posts come back for category pages in the tagged archives of case-study and webinar.

    I changed the 2nd if statement to is_home as the blog archive still needs to be filtered, and that removed the webinar/case-study tagged posted from /blog, but kept them visible on all category pages.

    Any thoughts? The first function worked great it just needed to keep those posts visible on any url that as the /?tag= at the end of the url

    #1059039
    Tom
    Lead Developer
    Lead Developer

    Hmm, I’m all out of ideas on this one, unfortunately.

    It might be worth asking over on something like https://wordpress.stackexchange.com/.

    I have a feeling we’re missing something super simple, I just can’t see what it is.

    The code I wrote does this:

    If we’re on the webinar or case-study archive, stop running the function.

    If we’re still here, we’re not on those archives, so check to see if we’re on another archive and don’t display posts with those tags.

    Does that logic sound right?

    #1059662
    Heather

    Yes I think that sounds around right – I think the weird thing is that on the case-study or webinar tagged archive, those URLs for category filters on those pages I think are still primarily a base category filter, because the url is still /blog/category/something/?tag=webinar like this, so anything you do to the category as a filter will affect this.

    But your logic is right.

    Is there a function where you can use a filter based on a url? like, if it is ?tag=webinar, show posts with that tag ID, if not, show posts without these id’s?

    #1059776
    Tom
    Lead Developer
    Lead Developer

    We could check for that tag in the URL and exclude the tags if it doesn’t exist:

    add_action( 'pre_get_posts', function( $query ) {
        $tag = isset( $_GET['tag'] ) ? true : false;
    
        if ( $query->is_archive() && ! $tag ) {
            $query->set( 'tag__not_in', array( 182, 180, 166 ) );
        }
    } );
    #1060747
    Heather

    Unfortunately that code also causes no filter to work, so all posts are visible across the site. Does this need to be combined with something?

    #1060855
    Tom
    Lead Developer
    Lead Developer

    What do you mean by no filters work? Like if you add ?tag=webinar to your URL it doesn’t do anything?

    #1060999
    Heather

    No – sorry, I know it’s a lot of back and forth so I will try to describe the breakdown again.

    The blog archive (“Posts Page”), should only be showing posts that do not have those tags, I had this filter already applied using this code:

     function exclude_posts( $query ) { 
    
    		if ( $query->is_home() ) {  
    	
    	 		$query->set( 'tag__not_in', array( 182, 180, 166 ) ); 
    	
    		}
    	 } 
    	add_action( 'pre_get_posts', 'exclude_posts' );

    What happens when I go to a category archive, such as /blog/category/devops – all posts, including ones with those tags, appear, so the tag not in filter is only working on posts page.

    I have specific sections for Case Studies and webinars to show only posts that are tagged as such. If you select a category on those pages, you will see a url that includes the ?tag=webinar at the end of it, after the category is listed.

    I need a filter that will have only posts that are NOT tagged with webinar or case study to show up not just on the posts page, but also each general category archive. But, they still need to show up on the case study and webinar pages linked above.

    The last piece of code you gave me caused all posts, regardless of their tag, to re-appear on the blog archive page, so no “filter” is being picked up by those tags. I went back to this code below:

    	add_action( 'pre_get_posts', function( $query ) {
    		if ( $query->is_archive( 'webinar' ) || $query->is_archive( 'case-study' ) ) {
    			return $query;
    		}
    	
    		if ( $query->is_home() ) {
    			$query->set( 'tag__not_in', array( 182, 180, 166 ) );
    		}
    	} );

    and now the post pages are getting filtered again, but all posts even if they have those tags are showing up on category pages that have /blog/category/(whatever category).

    I was hoping that we could find a way so that if the page as a url containing the ?tag=(whatever tag) at the end of it, it would show the posts, and if it didn’t, it won’t show them. So, posts that have a tag of say, webinar, should ONLY appear on the webinar tag archive, and any category archive that has the ?tag=webinar at the end of the url. Otherwise, it shouldn’t be shown.

    #1061184
    Tom
    Lead Developer
    Lead Developer

    WordPress itself doesn’t make this easy, unfortunately.

    In theory, this should work:

    add_action( 'pre_get_posts', function( $query ) {
        if ( $query->is_archive( 'webinar' ) || $query->is_archive( 'case-study' ) ) {
            return $query;
        }
    	
        if ( $query->is_home() || $query->is_archive() ) {
            $query->set( 'tag__not_in', array( 182, 180, 166 ) );
        }
    } );

    Basically, it’s doing this:

    1. If we’re on the webinar or case-study archive, do nothing.
    2. If we’re on the blog or in an archive, exclude posts with those tags.

    Without #1, #2 would exclude those tags from their own archives, but since we’ve done #1, #2 should only execute if we aren’t on those two archives.

    #1062017
    Heather

    Ah ok. Yeah it works fine on the blog but not the archives.

    Hypothetically, would this be easier to do if I limit things to categories? So instead of using tags, I make a category for webinar and case studies, and then have a post with a category of devops and webinar, can I filter it out that way? So blog and archive pages do not show anything with category of webinar, but only show it on webinar archive page.

    Is there a way to create a link that would be for two categories? similar to how there is a ?tag=webinar I could have a url that filters by webinar and devops?

    Or would it work the same way?

    #1062550
    Tom
    Lead Developer
    Lead Developer

    What if you used a custom post type, instead?: https://wordpress.org/plugins/custom-post-type-ui/

    That way you could completely separate webinars etc.. from your regular blog posts.

Viewing 10 posts - 31 through 40 (of 40 total)
  • You must be logged in to reply to this topic.