Specifically, is it valid to run a new WP_Query() during the plugins_loaded action?

  • A custom instance of WP_Query can run in almost any scenario on any hook depending on what you need to do. You'll need to add some context here, specially the code and error – Pieter Goosen Feb 3 '16 at 17:41
  • Users are only authenticated later than plugins_loaded and is only available on init which runs later than plugins_loaded – Pieter Goosen Feb 3 '16 at 19:05
  • I appreciate the input Pieter. However interpreting the BP error isn't really the problem, and I realised that it was just confusing the issue, so I have removed everything that doesn't relate to the real question: What is the earliest hook available for a new WP_Query, if plugins_loaded is not valid? – EpF Feb 3 '16 at 23:08
  • 1
    post types and taxonomies are registered on init, I'd guess that you can't reliably do any querying until after that, but maybe core types would work earlier. – Milo Feb 3 '16 at 23:16
  • @Milo - Thanks, that does make some sense. Weirdly though, I can successfully both insert and retrieve posts for a custom post type during plugins_loaded. Still, after looking at this: wordpress.stackexchange.com/questions/155108/… I tried dumping $wp_post_types and it only had core items. I find this very confusing, but I'm going to continue on the basis that init is the earliest completely reliable action to use. – EpF Feb 3 '16 at 23:55

As @Milo pointed out in the comments above, custom post types and taxonomies are registered on init, so this would seem to be the earliest hook available that is guaranteed to work for all content.

  • As my last comment is hidden below the fold above, I'll just add that in practice it is a bit confusing; I was able to both insert and retrieve custom-post-type posts on the plugins_loaded action - init just seems safer. – EpF Feb 4 '16 at 12:12
  • wp_query has very little to do with CPT registration as it runs on the DB. The results of the query might not be very useful but that depends how you want to use them. – Mark Kaplun Feb 4 '16 at 12:21
  • 1
    @MarkKaplun will a tax query succeed if it's not a registered taxonomy returned by get_taxonomies? I've noticed that invalid post types or taxonomies result in an impossible SQL condition, are those values not validated against registered types? – Milo Feb 4 '16 at 17:24
  • 1
    @Milo, not sure how taxonomies work, but wp_query should be a relatively easy query on the posts table and if the post_type in the table is "X" then querying for it should return it even if it is not registered as a post type. How will that impact any code after the query itself I guess depends on what you do with the results. – Mark Kaplun Feb 4 '16 at 17:45

You should not use any hook before wp_loaded unless it performs some initialization, or required for user authentication, therefor the earliest is wp_loaded

  • Never mind - seems I don't read too well! – EpF Feb 5 '16 at 12:59

This is template_redirect

Here : https://www.rarst.net/images/wordpress_core_load.png

add_action('template_redirect', 'yourFunction');
  • 1
    Can you elaborate why the earliest hook where it is valid to run a new WP_Query is this one? – kaiser Jun 15 '16 at 21:44
  • For the default WP_Query, in the core, it is the first hook after it. You can test an other query type in plugins_loaded. – madvic Jun 23 '16 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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