Sensei: Multiple PHP notices when viewing course archive page on certain themes

Created on 17 Jul 2019  路  2Comments  路  Source: Automattic/sensei

Steps to Reproduce

  1. Switch to the Divi or GeneratePress themes.
  2. Visit the course archive page.

What I Expected

No PHP notices to appear in debug.log.

What Happened Instead

debug.log is filled with PHP notices similar to the following:

PHP Notice:  Undefined property: WP_Post_Type::$post_name in /app/public/wp-includes/class-wp-query.php on line 3960
PHP Notice:  Undefined property: WP_Post_Type::$ID in /app/public/wp-includes/class-wp-query.php on line 3956
PHP Notice:  Undefined property: WP_Post_Type::$post_title in /app/public/wp-includes/class-wp-query.php on line 3958
PHP Notice:  Undefined property: WP_Post_Type::$post_name in /app/public/wp-includes/class-wp-query.php on line 3960
PHP Notice:  Undefined property: WP_Post_Type::$post_type in /app/public/wp-content/plugins/sensei-lms/includes/class-sensei-posttypes.php on line 906

The last notice is of particular importance as it's coming directly from Sensei LMS.

This was found while testing https://github.com/Automattic/sensei-wc-paid-courses/pull/146, which doesn't load the CSS file on Divi or GeneratePress because the following check fails. There may be other side effects that we're not aware of:
https://github.com/Automattic/sensei-wc-paid-courses/pull/146/files#diff-37b96f48999247da387df43ba2a8604cR65

I suspect that this may be related to theme compatibility, which alters queried_object.

Themes [Pri] Normal [Status] Accepted [Type] Bug

Most helpful comment

I took a quick look at this. It appears that this may be the result of not messing with queried_object quite enough 馃槄

In this code we create a dummy post object for rendering archive page content. Then we override the global $wp_query to seem like it has loaded that dummy post from the database, and then trick the theme into thinking that it should be rendering the "single post" template.

Although we do set several attributes of the new $wp_query in that code (including post), we do not actually set queried_object. So on the course archive page, get_queried_object() still returns the post type, even though the theme (i.e. Divi) is rendering the "single post" template. I believe this is why it looks like we are trying to access several WP_Post attributes on a WP_Post_Type object (e.g. post_name, ID, post_type).

Adding these two lines to the code linked above seemed to fix the problem for me:

$wp_query->queried_object = $post;
$wp_query->queried_object_id = $post->ID;

So that will be a good starting point for this one, but we'll want to test thoroughly to make sure that it doesn't have other implications.

All 2 comments

I took a quick look at this. It appears that this may be the result of not messing with queried_object quite enough 馃槄

In this code we create a dummy post object for rendering archive page content. Then we override the global $wp_query to seem like it has loaded that dummy post from the database, and then trick the theme into thinking that it should be rendering the "single post" template.

Although we do set several attributes of the new $wp_query in that code (including post), we do not actually set queried_object. So on the course archive page, get_queried_object() still returns the post type, even though the theme (i.e. Divi) is rendering the "single post" template. I believe this is why it looks like we are trying to access several WP_Post attributes on a WP_Post_Type object (e.g. post_name, ID, post_type).

Adding these two lines to the code linked above seemed to fix the problem for me:

$wp_query->queried_object = $post;
$wp_query->queried_object_id = $post->ID;

So that will be a good starting point for this one, but we'll want to test thoroughly to make sure that it doesn't have other implications.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gaurav1984 picture gaurav1984  路  4Comments

danjjohnson picture danjjohnson  路  7Comments

cleardesign picture cleardesign  路  5Comments

jeichorn picture jeichorn  路  6Comments

donnapep picture donnapep  路  5Comments