Archived topic
Random posts exclude (Stick to the top of the blog)
3 replies · Started by Mouhcine on March 3, 2022
Hello
I use this function to display random posts on archive pages. The code work very well but I would like to know if it's possible to exclude posts with (Stick to the top of the blog).
function shailan_post_order( $query ) {
if ( $query->is_archive ) {
$query->set( 'orderby', 'rand' );
}
}
add_action( 'pre_get_posts', 'shailan_post_order' );
Thank you
Hi there,
you can try this:
function shailan_post_order( $query ) {
if ( $query->is_archive ) {
$query->set( 'orderby', 'rand' );
$query->set('ignore_sticky_posts', true);
}
}
add_action( 'pre_get_posts', 'shailan_post_order' );
What that should do stop Sticky posts from being displayed at the top.
Hello David and thank you for your reply.
Indeed, I want the opposite. I would like to exclude posts with (Stick to the top of the blog) from the random posts so that they will appear at the top of archives tag pages.
I think you have talked about this https://generatepress.com/forums/topic/archive-sticky-post/#post-1942742
To sum up, I want to display sticky posts in the tag archive pages without them being affected by the random code.
Thank you
Hi there,
That's quite tricky to do as we'll have to get the id of the stickied post and use it for pre_get_post.
You'll have to do something like this.
https://wp-qa.com/store-sticky-post-s-ids-in-a-transient