Make WordPress Core

Opened 7 years ago

Last modified 6 years ago

#43294 new enhancement

Sticky class should be added regardless of where posts are queried

Reported by: selrond's profile Selrond Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords:
Focuses: Cc:

Description

post_class() function is only adding .sticky class when is_home() is true, basically

...if the query is for the blog homepage.

is_home() reference

Check out the code in get_post_class() function

512             // sticky for Sticky Posts
513             if ( is_sticky( $post->ID ) ) {
514                     if ( is_home() && ! is_paged() ) {
515                             $classes[] = 'sticky';
516                     } elseif ( is_admin() ) {
517                             $classes[] = 'status-sticky';
518                     }
519             }

This behavior complicates recognizing sticky post when using custom queries, as they're mostly used outside of homepage, which fails is_home() check, requiring extra work to add .sticky class where it should be.

I suggest removing the is_home() check from the condition in get_post_class() function.

Change History (3)

#1 follow-up: @birgire
7 years ago

  • Component changed from Query to Posts, Post Types

@Selrond Welcome to WordPress core trac.

The get_post_class() function was introduced in 2.7 and there the sticky part was already with a is_home() check:

// sticky for Sticky Posts
if ( is_sticky($post->ID) && is_home())
    $classes[] = 'sticky';

The sticky posts are injected within the WP_Query::get_posts() src:

// Put sticky posts at the top of the posts array
$sticky_posts = get_option( 'sticky_posts' );
if ( $this->is_home && $page <= 1 && is_array( $sticky_posts ) && ! empty( $sticky_posts ) && ! $q['ignore_sticky_posts'] ) {
    // ...
}	

where the $this->is_home && $page <= 1 restrictions looks very similar to the:

if ( is_home() && ! is_paged() ) { 

check in get_post_class() in 4.9.

So the is_home() check looks normal here or maybe I'm misunderstanding your proposal @Selrond ?

#2 in reply to: ↑ 1 @Selrond
7 years ago

@birgire my issue is really that .sticky should be added to sticky posts regardless of how they are queried (default query or custom query).

Current implementation only adds .sticky class to sticky posts when queried from default query (due to the is_home() check.

I propose removing is_home() check in post-template.php which would enable adding .sticky class to posts queried from custom queries.

#3 @pento
6 years ago

  • Version trunk deleted
Note: See TracTickets for help on using tickets.