admin管理员组

文章数量:1122832

On a category archive page, in a filter called from a block outside a loop, I noticed that the get_post function with a null first parameter returns the first loop's post.

Is it supposed to return such a value ? I would have imagined that the global $post variable should have a value only in a loop or on a post page.

On a category archive page, in a filter called from a block outside a loop, I noticed that the get_post function with a null first parameter returns the first loop's post.

Is it supposed to return such a value ? I would have imagined that the global $post variable should have a value only in a loop or on a post page.

Share Improve this question edited Apr 15, 2024 at 11:52 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Apr 15, 2024 at 11:17 challetchallet 1114 bronze badges 1
  • Can you add your code snippet here please – Pratik bhatt Commented Apr 15, 2024 at 11:51
Add a comment  | 

1 Answer 1

Reset to default 1

Yes, this is how it is supposed to work. The global $post variable is set quite early in the page load, at the wp hook. As you can see from the hook order, this is before the loop starts.

Basically the $post object exists independent from its actual content in the database, which you retrieve during the loop. It's specifically meant to be accessible all the time during page generation.

本文标签: Global post value outside the loop