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

filters on postobject may not work as expected. global post object is not setup. #718

Closed
jespervnielsen opened this issue Mar 11, 2019 · 6 comments
Milestone

Comments

@jespervnielsen
Copy link

https://github.com/wp-graphql/wp-graphql/blob/develop/src/Type/Object/PostObject.php#L267

apply_filters( 'the_excerpt', $excerpt );
this is in a way correct.
But since the global post object isnt't setup, then there may be scenarios, where it will be the wrong post.

a way to solve this, could be to use

   global $post;
   $post = $post_object;
   
   setup_postdata( $post );
$excerpt = apply_filters( 'get_the_excerpt', $excerpt, $post );
$excerpt =apply_filters( 'the_excerpt', $excerpt );
wp_reset_postdata();
return $excerpt;

The same issue is relevant for "the_content" and "the_title"

@esamattis
Copy link
Collaborator

I believe this will be fixed by

#714

Care to try manually merging it and checking if it fixes it for you?

@jespervnielsen
Copy link
Author

@epeli i can confirm that #714 solves this.

i did a checkout of 714, and the issues is solved.

And i makes sense to fill the global post object, on a field level, as the PR does.

@esamattis
Copy link
Collaborator

Thanks. I hope we can get it merged soon.

In the mean time if you don't want to run forked version you should be able to do:

add_action('graphql_before_resolve_field', function($source) {
  if (is_a( $source, 'WP_Post' ) ) {
    $GLOBALS['post'] = $source;
    setup_postdata( $source );
  }
} );

@jasonbahl
Copy link
Collaborator

@jespervnielsen @epeli reviewing the PR now. Hoping to merge today. 😄

@esamattis
Copy link
Collaborator

Merged 💪

@jasonbahl
Copy link
Collaborator

closed by #714

@jasonbahl jasonbahl added this to the 0.2.3 milestone Mar 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants