Woocommerce pre_get_posts causing problems

I have an issue at hand. I am using add_action('pre_get_posts','test_hook') inside my theme’s functions.php and call it inside the products page: do_action('pre_get_posts');

The thing is that when I call that action, other functions hooked into pre_get_posts are called and I get the following error: Call to a member function is_main_query() on a non-object in wc-deprecated-functions.php

Read More

I looked up that code and it looks like:

add_action( 'pre_get_posts', 'wc_shop_order_status_backwards_compatibility' );
function wc_shop_order_status_backwards_compatibility( $q ) {
   if ( $q->is_main_query() ) {
      return;
   }
//...and it goes on
}

I need to hook my function without affecting this ones, but I don’t really see how. If I comment this function, another one hooked into pre_get_posts returns the same error.

PS: My function is empty. The way I see it, even without creating my own hook and only by calling the hook within that page (archive_products.php) creates this error.

Related posts

Leave a Reply

1 comment

  1. You shouldn’t call do_action('pre_get_posts') on your page…it gets called automatically after the query variable object is created, but before the actual query is run. See the documentation for a quick explanation.

    More context: NONE of the core WordPress Actions need to be explicitly called (and none of them are called with do_action()). While something like wp_head(); is called in the template, this is actually the wp_head() function, not the action.

    WordPress is confusing sometimes, because it duplicates names of actions, hooks, and functions.