Site logo

Archived topic

WP Show Posts: Sticky posts NOT ignored when Taxonomy selected

14 replies · Started by Rafał on September 12, 2021

Viewing posts 1–15 of 15

Hi Rafal,

The issue has something to so w/ WordPress' core function WP_query().

It doesn't allow sticky posts for archive pages (queries w/ specified taxonomy).

You can even test this out yourself by assigning a sticky post and check if it goes first on the list on its own category/tag archive page.

But as for workaround, I'd steer away from using WordPress default's way of assigning sticky posts. I'd use a custom field to assign if a post is a sticky post and then use this as meta_key and meta_value for orderby on the query.

also, I'd make 2 of the same category post lists stacking on top of each other.

The first list will be strictly for posts of the same category that have "sticky" indicated through its custom field.

The second list will be strictly for posts of the same category that excludes posts that have "sticky" indicated through its custom field to avoid duplicate posts.

Hi Elvin,
Thank you for your clever workaround. I'm afraid it would be handy in a small project, but not at website where many posts are published each day, by many users.
If only we could stay with Meta key / Meta value for sticky posts, but we have to assign the meta for every post - true or false - haven't we?

Please, let me come back to the WP "native-sticky-inconvenience-in-archives". I admit, it takes place in standard outputs.

However, what do you think about the new Query Block (from WP 5.8), which can maintain sticky posts very well. You may: include, exclude, or show only sticky posts!

Unfortunately, the biggest performance problem is here—you can't choose featured image size (only the full one).

On the other hand, Latest Posts widget allows to choose image size, but lacks other crucial options.

To clarify, do we have to simply include the sticky post or must we have it show as the first post on the list?

If its just including sticky posts to the post list, that's something we can do w/ WPSP.

But if its making the sticky post as the first post on the category query. I don't think wp_query does that. Even the Query block can't do it. (it will include the sticky post but it will follow the orderby value of the query and NOT put the sticky post on the top of the list)

In my case the aim is to exclude sticky posts in a single category list (there are many blocks - per each category). Sticky post is highlighted in a separate block.

Ah in that case try this:

Use our beta version of the WPSP plugin - https://github.com/tomusborne/wp-show-posts/tree/release/1.2

and then do this PHP filter. (replace 10 on the line if ( 10 === (int) $settings['list_id'] ) with your target WPSP ID)

add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
    if ( 10 === (int) $settings['list_id'] ) {
		$sticky = get_option( 'sticky_posts' );
		$args['post__not_in'] = $sticky;
    }

    return $args;
}, 20, 2 );

That's it!!!
Works for me :)
Thank you, Elvin!

PS
I'd recommend to implement your filter into official release of WPSP, under the hood of checkbox "Ignore sticky posts".

PS
I’d recommend to implement your filter into official release of WPSP, under the hood of checkbox “Ignore sticky posts“.

Yeah that's actually what its supposed to do but it doesn't apply when the query has taxonomy. It's toggling true or false on 'ignore_sticky_posts'.

I've logged it as an issue on our GitHub page for the next patch. :D
https://github.com/tomusborne/wp-show-posts/issues/41

Thanks a lot!

Regards & respect

No problem. Glad to be of any help. :D

If someone would like to go along my scenario:
WPSP with ID 888 shows sticky only, all other lists exclude sticky posts.

add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
	if ( 888 != (int) $settings['list_id'] ) {
		$sticky = get_option( 'sticky_posts' );
		$args['post__not_in'] = $sticky;
	}
	if ( 888 === (int) $settings['list_id'] ) {
		$sticky = get_option( 'sticky_posts' );
		$args['post__in'] = $sticky;
	}
	return $args;
}, 20, 2 );

Not sure what's the question here. Is the code not working?

Can you explain what's the aim of the code? Let us know.

Is working! Thanks to you, Elvin.
Just share my solution :)

Oh nice one! Thanks for sharing it with us. :D

This archived topic is closed to new replies.