WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 9 months ago

#14825 closed defect (bug) (wontfix)

'Sticky' Posts from excluded category still included in WP_Query results

Reported by: newmediarts Owned by: ryan
Milestone: Priority: normal
Severity: major Version: 3.0.1
Component: Query Keywords: has-patch dev-feedback
Focuses: Cc:

Description

Hi,

I want to have a list of articles that exclude posts in a category I have called "Stories." Some articles in the Stories category are marked as "sticky".

$cat_id = get_cat_id("Stories");
$query = new WP_Query("cat=-{$cat_id}&posts_per_page=10&caller_get_posts=0");

However, this still returns articles from the Stories category, but only those that are marked as "sticky".

I've taken a look at the source code of wp-includes/query.php, and it seems that what's happening is that it prepends all "sticky" posts that were not in the initial query results, regardless of category. (There is only logic implemented that honours the post_type.)

I would appreciate it this bug be addressed and released in an upcoming WordPress release.

Thanks.

Attachments (1)

14825.diff (1.1 KB) - added by garyc40 3 years ago.
honor $where and $join of the main query when querying for sticky posts

Download all attachments as: .zip

Change History (8)

comment:1 markjaquith3 years ago

  • Owner set to ryan
  • Status changed from new to reviewing

Seems reasonable to me that tax exclusions should exclude sticky posts in that tax. Ryan?

comment:2 newmediarts3 years ago

Any update on this issue?

garyc403 years ago

honor $where and $join of the main query when querying for sticky posts

comment:3 garyc403 years ago

  • Keywords has-patch dev-feedback added; sticky wp_query bug removed

I attached 14825.diff, hoping this would get the ball rolling.

I make the query for sticky posts honor $where and $join clauses of the parent query. Really don't know whether this is the expected behavior that Ryan has in mind, or if it has any performance implications, but it works fine in my testing environment and solves the reporter's issue.

comment:4 newmediarts3 years ago

  • Cc newmediarts added

This patch seems to be working fine for me—thanks, Gary!

comment:5 newmediarts3 years ago

Just a question:
If you wanted to try reducing this down to one query, wouldn't it be easier to do something like the following:

Given get_option("sticky_posts") == array(10, 12, 16):

SELECT * from wp_posts 
WHERE wp_posts.post_type = "post"
ORDER_BY wp_posts.ID IN (10, 12, 16) DESC, # sticky_posts IDs
wp_posts.post_date DESC # regular orderby clause

instead of doing two separate SQL queries? (simplified code used above)

comment:6 scribu3 years ago

I guess it could be done in a single query, but it's not so simple. Should open a new ticket for that.

comment:7 wonderboymusic9 months ago

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

Stickies are a very basic construct: throw these posts at the top of the loop on the homepage. The only way to exclude posts is via post__not_in. The sticky query would be way too complicated (and would have to be completely re-thought) if it had to follow all of the rules of the query instance that contains it.

Note: See TracTickets for help on using tickets.