-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Description
Found an interesting issue where using is_shop() inside the pre_get_posts hook will cause numerous PHP notices when loading a site with a static homepage.
To reproduce:
- Spin up a fresh WP + WooCommerce
- Create a standard page and set it as the static homepage under Settings->Reading
- Import dummy data (just so we can see something on the shop page)
- Turn WP_DEBUG on
- Add the following function to your functions.php file (just some sample code to demo the problem)
add_action( 'pre_get_posts', 'do_something_awesome', 1 );
function do_something_awesome() {
if (is_shop()) {
error_log( 'hello shop :)' );
}
}
Note the PHP notices showing:
Notice: Trying to get property of non-object in /xxxx/wp-includes/query.php on line 4529
Notice: Trying to get property of non-object in /xxxx/wp-includes/query.php on line 4531
Notice: Trying to get property of non-object in /xxxx/wp-includes/query.php on line 4533
This problem occurs with older Woo versions and on any theme in my testing, but the above was tested on fresh WP install with stock WooCommerce and default theme.
For the record, the is_shop function actually does work fine in pre_get_posts (visit the actual shop page and you'll see my error_log from the function above), but just not when you have a static homepage set.
I've tried several workarounds for not using is_shop, but it's incredibly hacky and I'd just prefer to use the proper function which should work without sending out a bunch of notices.