Make WordPress Core

Opened 8 years ago

Closed 7 weeks ago

#43294 closed enhancement (wontfix)

Sticky class should be added regardless of where posts are queried

Reported by: selrond's profile Selrond Owned by:
Milestone: 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 (5)

#1 follow-up: @birgire
8 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
8 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
7 years ago

  • Version trunk deleted

This ticket was mentioned in Slack in #core by sirlouen. View the logs.


7 weeks ago

#5 @SirLouen
7 weeks ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

As @birgire suggested, this template function is made ad hoc in combination with get_posts for a default behavior. If you want a custom behavior, there are plenty of hooks where you can give further conditions to add back the sticky classes you never got because of that default is_home condition.

Altering the default condition could cause unexpected variations in pages that could actually be using sticky styling and at the same time have other custom styles in places outside of home and custom queries, as you are proposing.

For this reason, this cannot be fixed that trivially, and there is a clear workaround, not hard to apply.

Note: See TracTickets for help on using tickets.