3

i have seen $wp_query and $wp_the_query in many plugins and themes. for me these two are just same thing with no difference. i am not looking to either use or get the values from $GLOBALS.

what is the actual difference between these two objects and when to use them? What is the proper action /filter hook for getting and reading values from $wp_query and $wp_the_query.

0

1 Answer 1

9

You should read in this answer it explains it in depth.

in short,

  • $wp_query is the variable that holds a copy of $wp_the_query global query that could/can be modified by plugins and themes
  • $wp_the_query is the variable that holds an unmodified copy of the global query object and used to reset the $wp_query object when we call wp_reset_query()

I think you now understand as to why it makes sense to have both available to us.

4
  • that's what i was looking for. can you suggest safe way to access wp_the_query from plugins?
    – Anwer AR
    Mar 21, 2017 at 11:00
  • I'm not sure I understand what you mean by safe... but $GLOBALS[ 'wp_the_query' ] should give you access to that variable.
    – bynicolas
    Mar 22, 2017 at 3:32
  • i mean that variable is not immediately accessible from plugin. it does not work with init action hook.
    – Anwer AR
    Mar 22, 2017 at 9:19
  • 1
    Oh ok, I believe this is normal behaviour because your hook is too soon. The query is not set up yet at init. Try a later hook like wp or template_redirect, you should have access to the variable. Check out this link for reference on hook sequence.
    – bynicolas
    Mar 22, 2017 at 11:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.