- This topic has 12 replies, 3 voices, and was last updated 4 years, 5 months ago by Leo.
-
AuthorPosts
-
October 28, 2019 at 12:09 pm #1047146Heather
Hi there,
How can I exclude posts that have a specific tag from the main blog archive page? Ie: If I have a tag of ‘happy’ I add to some posts, I do not want any posts with the ‘happy’ tag to show up in the blog archive/”posts” page.
I used this code to exclude the tag by tag ID but it didn’t work.
function exclude_tag( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'tag', '179' ); } } add_action( 'pre_get_posts', 'exclude_tag' );
October 28, 2019 at 12:11 pm #1047151HeatherI also tried it with a ‘-‘ sign in front of the id, so ‘-179’ and that did not work either
October 28, 2019 at 12:53 pm #1047181LeoStaffCustomer SupportHi there,
Can you try this?
add_filter( 'pre_get_posts', function( $query ) { if ( $query->is_home() && $query->is_main_query() ) $query->set( 'tag', '-19' ); } return $query; } );
October 28, 2019 at 1:04 pm #1047186HeatherThe return $query gives this error:
If called from within a function, the return() statement immediately ends execution of the current function, and returns its argument as the value of the function call. return() will also end the execution of an eval() statement or script file.
If called from the global scope, then execution of the current script file is ended. If the current script file was include()ed or require()ed, then control is passed back to the calling file. Furthermore, if the current script file was include()ed, then the value given to return() will be returned as the value of the include() call. If return() is called from within the main script file, then script execution ends. If the current script file was named by the auto_prepend_file or auto_append_file configuration options in php.ini, then that script file’s execution is ended.
If I get rid of it, my blog archive page just says “ready for your first post?”, so it seems to eliminate all posts.
Is there a way to target it via slug? I have “blog” set to my “post page” in my Reading settings.
October 28, 2019 at 1:08 pm #1047188LeoStaffCustomer SupportTry this:
function exclude_tag_home( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'tag', '-19' ); } return $query; } add_filter( 'pre_get_posts', 'exclude_tag_home' );
October 29, 2019 at 7:37 am #1047723HeatherUnfortunately that does the same thing where it eliminates all posts from the blog archive. Is the blog archive dictated by index.php?
Is there a way to target the index.php file or the “posts” page? Maybe that code is targeting that page I’m not sure, I just see home so I keep thinking its the home page.
Thanks
October 29, 2019 at 3:19 pm #1048250TomLead DeveloperLead DeveloperOctober 30, 2019 at 8:04 am #1048908HeatherYes, that worked! Thank you!
October 30, 2019 at 9:02 am #1048972TomLead DeveloperLead DeveloperYou’re welcome ๐
November 2, 2019 at 9:04 am #1051433HeatherHi, I realized I needed something else with this –
So I have 6 categories and 4 tags to differentiate posts (webinar, case-study, etc). I noticed that, although these posts are now hidden on the blog archive page, I also need them hidden within the archive category page as well.
I have specific pages to show the tag archive for these, and specific urls for categories within those tag archives to filter by cat + tag. So, a breakdown of URL’s:
/blog (blog archive/posts page)
/blog/category/devops (devops category archive page <- currently displays everything with that category regardless of tag)
/blog/tag/webinar (webinar tag archive page <- displays everything with the tag of webinar)
/blog/category/devops/?tag=webinar (an archive page that displays only posts with both the category of devops and tag of webinar)
I am using the code below to exclude webinar and case-studies from the blog archive page, but what would I add to this to ensure that posts with these tags are excluded on the category archive pages, but will still appear in the urls of the last two shown – tag archive and tag+cat (devops + webinar). Is this possible?
function exclude_posts( $query ) { if ( $query->is_home() ) { $query->set( 'tag__not_in', array( 182, 180, 166 ) ); } } add_action( 'pre_get_posts', 'exclude_posts' );
November 2, 2019 at 1:17 pm #1051571LeoStaffCustomer SupportHmm that sounds quite complicated and I’m not sure if it’s possible.
Have you tried asking on a forum like Stackoverflow?
https://wordpress.stackexchange.com/This isn’t really a theme question.
November 3, 2019 at 9:09 am #1052125HeatherOk, I guess the better question is how can I change this to only show posts of a specific tag. Would I just change the tag__not_in to tag_in?
November 3, 2019 at 9:59 am #1052186LeoStaffCustomer Supporttag__in
is worth a try.Maybe something like this?
https://wordpress.stackexchange.com/questions/174403/set-homepage-to-only-display-posts-from-one-tag -
AuthorPosts
- You must be logged in to reply to this topic.