Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sticky Posts are not being handled anymore in 0.3.0 #786

Open
mwidmann opened this issue Apr 8, 2019 · 10 comments · May be fixed by #863
Open

Sticky Posts are not being handled anymore in 0.3.0 #786

mwidmann opened this issue Apr 8, 2019 · 10 comments · May be fixed by #863
Labels
Component: Query ObjectType: Post Issues related to the Post object type in WP Status: Discussion Requires a discussion to proceed Type: Bug Something isn't working
Projects

Comments

@mwidmann
Copy link

mwidmann commented Apr 8, 2019

At first I thought, that the addition of

$query_args['ignore_sticky_posts'] = true;

brought up this error, but it seems to be way deeper than this. It appears that since 0.3.0 fetching the posts related to a query occurs in two steps: first the IDs of the posts are gathered, then the posts for the given ids are loaded.

Because of this change, sticky posts are never handled, as the querying for sticky posts is performed at a later time in wp-query.

https://github.com/WordPress/WordPress/blob/94b592ac684d7cc9a82b5ba42161d320a4c329f4/wp-includes/class-wp-query.php#L2906-L2916

When fields is set to ids in the query vars, the query returns before fetching and adding the sticky posts which only happens after line 3077 in class-wp-query.
https://github.com/WordPress/WordPress/blob/94b592ac684d7cc9a82b5ba42161d320a4c329f4/wp-includes/class-wp-query.php#L3077

Thus sticky posts will never be handled.

To be honest I have no clue how to solve this, as this requires far deeper knowledge for the way wp-graphql loads the data, so at the moment I'm quite overwhelmed with how to solve this.

@jasonbahl
Copy link
Collaborator

@mwidmann ah, you know what, I think we set ignore_sticky_posts to true by default as part of the model-layer branch, before you had your PR in.

So now we have it here: https://github.com/wp-graphql/wp-graphql/blob/develop/src/Data/Connection/PostObjectConnectionResolver.php#L117

And here: https://github.com/wp-graphql/wp-graphql/blob/develop/src/Data/Connection/PostObjectConnectionResolver.php#L152-L158

The latter is from your PR. . .the former is from the model-layer branch, prior to your PR.

I think we might want to actually remove the former and stick with your PR.

So that sticky posts will be respected for the first query, but not the subsequent 🤔

If you remove it from here does it bring back the behavior you expect?

@jasonbahl
Copy link
Collaborator

@mwidmann so, as far as how GraphQL loads data, it actually does a very similar thing to WP_Query.

When you execute a WP_Query, it makes one initial query for just the IDs, then it looks to see if those IDs are in the cache or not. If they are cached, it pulls the object from the cache. If they're not, it does another SQL request to get the full object.

WPGraphQL is doing the same thing, just spread across multiple WP_Queries for more improved performance.

Any WP_Query argument should still work the same as it would normally, including ingnore_sticky_posts

The issue, in this case, is just that we had added it as a default arg on the feature branch before you added your version (which is more elegant than just turning off sticky posts altogether 😂)

@jasonbahl
Copy link
Collaborator

I think if you can confirm removing the instance that sets it to true restores the behaviour you would expect, you can open a PR for that and we can get it merged.

@mwidmann
Copy link
Author

mwidmann commented Apr 9, 2019

@jasonbahl sadly this doesn't solve the issue as it lies deeper than this. The querying of the posts to fetch has been changed, so that you first only fetch the ids of the posts that would be fetched for the query and then, based on this list, you fetch the actual post objects for the keys you gathered.

When you pass in the fields => ids query_param when fetching the posts, wp-query handles this case much sooner and returns way earlier than where the sticky posts are added. I linked the lines in 5.1.1 source code of wp-query.

When you then load the list of items based on the ids you idenfied in the first query the original context is missing, so removing ignore_sticky_posts in PostObjectsLoader wouldn't solve this issue either.

The issue has been introduced in de3dd63.

I'm too unfamiliar with the new DataLayer's functionality to figure out what might be a good solution to it, sadly.

jasonbahl added a commit to jasonbahl/wp-graphql that referenced this issue May 23, 2019
- This introduces a filter `graphql_include_sticky_posts` which will include sticky posts in the first query in a post connection query if the filter is set to true. Subsequent pages of a paginated query will not include the sticky post at the top of the results.
jasonbahl added a commit to jasonbahl/wp-graphql that referenced this issue May 23, 2019
- This introduces a filter `graphql_include_sticky_posts` which will include sticky posts in the first query in a post connection query if the filter is set to true. Subsequent pages of a paginated query will not include the sticky post at the top of the results.
@jasonbahl jasonbahl linked a pull request May 23, 2019 that will close this issue
@jasonbahl jasonbahl added this to To Prioritize in 4: Actionable Issues Aug 7, 2019
@TylerBarnes
Copy link
Collaborator

We're thinking maybe adding a field isSticky for some contexts (easy), and being able to sort by sticky in others (harder). More discussion is needed though for sure

@TylerBarnes TylerBarnes added this to To do in V1 via automation Nov 3, 2019
@TylerBarnes TylerBarnes added this to the 1.0 release milestone Nov 3, 2019
@CodeProKid CodeProKid added Type: Bug Something isn't working Status: Discussion Requires a discussion to proceed labels Nov 19, 2019
@jasonbahl jasonbahl modified the milestones: 1.0 release, v0.7.0 Jan 14, 2020
@CodeProKid CodeProKid added Component: Query ObjectType: Post Issues related to the Post object type in WP labels Jan 17, 2020
@CodeProKid
Copy link
Member

There's an open PR attached here, but there are some nuanced details to this. Needs deeper discussion.

@jasonbahl jasonbahl removed this from the v0.7.0 milestone Jan 22, 2020
@jasonbahl jasonbahl added this to the 1.0 release milestone Apr 27, 2020
@jasonbahl jasonbahl removed this from the 1.0 release milestone Jun 24, 2020
@stale
Copy link

stale bot commented Aug 3, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Aug 3, 2022
@stale
Copy link

stale bot commented Sep 2, 2022

This issue has been automatically closed because it has not had recent activity. If you believe this issue is still valid, please open a new issue and mark this as a related issue.

@stale stale bot closed this as completed Sep 2, 2022
@justlevine justlevine reopened this Sep 2, 2022
@stale stale bot removed the stale label Sep 2, 2022
@stale
Copy link

stale bot commented Dec 1, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Stale? May need to be revalidated due to prolonged inactivity label Dec 1, 2022
@jasonbahl jasonbahl removed the Stale? May need to be revalidated due to prolonged inactivity label Dec 1, 2022
@jasonbahl jasonbahl added the not stale Short-circuits stalebot. USE SPARINGLY label Dec 1, 2022
@justlevine justlevine removed the not stale Short-circuits stalebot. USE SPARINGLY label Mar 3, 2023
@PSMJonas PSMJonas mentioned this issue Oct 15, 2023
3 tasks
@justlevine
Copy link
Collaborator

From today's Office Hours:

Assuming #863 can't be dusted off to work well enough with cursor pagination, we could use an isSticky where arg and relegate this handling to the frontend. E.g.

query StickyPosts( $first: Int, $after: ID  ) {
  stickyPosts: posts( first: $first, after: $after, where: { isSticky:true } ) {
    pageInfo {
      ...PageInfoFrag
    }
    nodes {
      ...PostFrag
    }
  }
}

query Posts( $first: Int, $after: ID ) {
  posts( first: $first, after: $after, where: { isSticky: false } ) {
    pageInfo {
      ...PageInfoFrag
    }
    nodes {
     ... postFrag
   }
 }

and then paginate via the frontend, first through StickyPosts until pageInfo.hasNextPage is false, and then through Posts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Query ObjectType: Post Issues related to the Post object type in WP Status: Discussion Requires a discussion to proceed Type: Bug Something isn't working
Projects
Status: 🗺 Planned
V1
  
To do
Development

Successfully merging a pull request may close this issue.

5 participants