Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jkohlbach opened this issue Mar 31, 2016 · 7 comments

Comments

Projects
None yet
6 participants
@jkohlbach
Copy link

commented Mar 31, 2016

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.

@mikejolley

This comment has been minimized.

Copy link
Member

commented Mar 31, 2016

@jkohlbach

This comment has been minimized.

Copy link
Author

commented Apr 1, 2016

Hey Mike, are you able to weigh in on that ticket? It's effecting WooCommerce just as much as it's effecting other plugin devs too and I think your word would go a bit further than mine.

Looks like it already has a patch and unit test ready to go via this ticket:
https://core.trac.wordpress.org/ticket/27015

Just hasn't made it into a release for some reason?? Very odd.

@claudiosanches

This comment has been minimized.

Copy link
Member

commented Apr 1, 2016

Maybe it can work:

add_action( 'pre_get_posts', 'do_something_awesome', 1 );

function do_something_awesome( $query ) {
    if ( is_admin() ) {
        return;
    }

    if ( $query->is_post_type_archive( 'product' ) || $query->is_page( wc_get_page_id( 'shop' ) ) ) {
        error_log( 'hello shop :)' );
    }
}
@jkohlbach

This comment has been minimized.

Copy link
Author

commented Apr 1, 2016

Actually @claudiosmweb the workaround is a bit more involved. We've tested this pretty thoroughly:

add_action( 'pre_get_posts', 'is_shop_workaround_demo', 1 );

function is_shop_workaround_demo( $query ) {
    $front_page_id        = get_option( 'page_on_front' );
    $current_page_id      = $query->get( 'page_id' );
    $shop_page_id         = apply_filters( 'woocommerce_get_shop_page_id' , get_option( 'woocommerce_shop_page_id' ) );
    $is_static_front_page = 'page' == get_option( 'show_on_front' );

    // Detect if it's a static front page and the current page is the front page, then use our work around
    // Otherwise, just use is_shop since it works fine on other pages
    if ( $is_static_front_page && $front_page_id == $current_page_id  ) {
        error_log( 'is static front page and current page is front page' );
        $is_shop_page = ( $current_page_id == $shop_page_id ) ? true : false;
    } else {
        error_log( 'is not static front page, can use is_shop instead' );
        $is_shop_page = is_shop();
    }

    // Now we can use it in a conditional like so:
    if ($is_shop_page) {
        error_log( 'this is the shop page' );
    }
}

If anyone comes along here looking for a solution, please feel free to use the above workaround code in the meantime until WP core gets this fixed.

@TimbreDesign

This comment has been minimized.

Copy link

commented May 5, 2017

Seems like this still has not been fixed?
I'm running into this on the front page:
Trying to get property of non-object ~> wp-includes/class-wp-query.php:3750 (& 3752/3754)
WP_Query->is_page() ~> wp-includes/class-wp-query.php:3688

Update:
The deeper issue is here: https://core.trac.wordpress.org/ticket/21790

@brunompbarbosa

This comment has been minimized.

Copy link

commented Oct 19, 2018

Just as FYI, on October 2018 this core issue is still unresolved and this code saved me!

@kLOsk

This comment has been minimized.

Copy link

commented Oct 25, 2018

I see also different very strange behaviour here.
error_log($query->get( 'page_id' ));
This, within 'pre_get_posts' returns 0 for every WC page? Has anyone seen this and might have an idea why this happens? pre_get_posts being called in functions.php of a theme.

I fnally managed to solve this, so instead of using is_shop I use is_post_type_archive( 'product' ). Maybe that helps someone...


function some_function_name( $query ) {
	if ( ! is_admin() && $query->is_main_query() ) {

		if ( is_post_type_archive( 'product' ) || is_product_category() || is_product_tag() ) {
		 	// User ID to only display products of this person
			$query->set( 'author', 3 );
		}

	}
}
add_action( 'pre_get_posts', 'some_function_name' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.