Archived topic
archive sticky post
24 replies · Started by Mike on December 12, 2019
Hi Benjamin,
If it's for ALL the tag archives, you'll have to do it like this:
function exclude_posts( $query ) {
if ( is_feed() ) {
return $query;
}
if ( $query->is_tax() ) {
$query->set( 'tag__not_in', array( 2414 ) );
}
return $query;
}
add_action( 'pre_get_posts', 'exclude_posts' );
I mean just on a specific tag archive page. In this case I have a tag called LMUTW (id 2178), and I have created a matching tag called LMUTW Spotlight (2414). I'd like to exclude any post tagged LMUTW Spotlight from the LMUTW archive, so I can place the spotlight post in a Showpost/spotlight box at the top of the page.
Try this:
function exclude_posts( $query ) {
if ( is_feed() ) {
return $query;
}
if ( $query->is_tag(2178) ) {
$query->set( 'tag__not_in', array( 2414 ) );
}
return $query;
}
add_action( 'pre_get_posts', 'exclude_posts' );
This checks if the current page is an archive page of post tag ID 2178. If yes, it will exclude posts w/ tag id 2414.
Ah, that's just the outcome I want! But it's throwing a fatal error for the site.... :-/
Which may be because of another, identically named function. I have renamed this new one to function exclude_lmutwposts, however it doesn't seem to be doing the trick :-p https://newsmigstg.wpengine.com/tag/lmutw/
Can you try this instead?
function exclude_posts( $query ) {
if ( is_feed() ) {
return $query;
}
if ( $query->is_main_query() && is_tag( 2178 ) ) {
$query->set( 'tag__not_in', array( 2414 ) );
}
return $query;
}
add_action( 'pre_get_posts', 'exclude_posts' );
This one should work 100%.
Sorry for spamming you. User error. I needed to rename it in two places. Done, and seems to work! Thank you!
Sorry for spamming you. User error. I needed to rename it in two places. Done, and seems to work! Thank you!
Ah you probably had a duplicate function name. Glad you got it sorted. No problem.
This is pretty cool idea. Amazing you got it to work.
@Brad, let us know if you need help with something like this as well.