Skip to content

is_shop() causes PHP notices on pre_get_posts hook when loading static homepage #10625

@jkohlbach

Description

@jkohlbach

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:

  1. Spin up a fresh WP + WooCommerce
  2. Create a standard page and set it as the static homepage under Settings->Reading
  3. Import dummy data (just so we can see something on the shop page)
  4. Turn WP_DEBUG on
  5. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions