I have a function retrieving ID's of posts by very specific means.
I need to be able to set the
global $post in order to use functions like
the_content(), which does not allow an ID as a parameter.
How can I achieve this?
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.Sign up to join this community
To work with posts outta loop try using:
to get a post with a specific ID out of the loop,when you do this,the global object $post will hold the post which you'v just selected.Now you can get the content using :
You may use a one-liner to reset the global post object.
setup_postdata( $GLOBALS['post'] =& $custom_post_object );
When you finish your template or loop be sure to reset the global post so you won't break other templates further down the stack.
Note: PHPCS with WordPress standards will complain about overriding the global post so if you're using the WordPress standards you'll need to disable it for the line like so:
setup_postdata( $GLOBALS['post'] =& $post ); //phpcs:ignore