WordPress.org

Ready to get started?Download WordPress

Forums

Yet Another Related Posts Plugin (YARPP)
[Plugin: Yet Another Related Posts Plugin] Resolving "Notice: Trying to get property of non-obj (4 posts)

  1. robbyslaughter
    Member
    Posted 3 years ago #

    If you put WordPress into debug mode with:

    define('WP_DEBUG', true);

    You may get the following warning

    "Notice: Trying to get property of non-object in /yourpath/wp-content/plugins/yet-another-related-posts-plugin/includes.php on line 274"

    This appears to be caused by this single line of code:

    $type = ($post->post_type == 'page' ? array('page') : array('post'));

    My interpretation of the above code is that it attempts to set the variable $type based on the object $post. However, $post may not actually be an object, if for example you are viewing a regular WordPress page.

    By rewriting this code with the an is_object($post) test, you can avoid this notice.

    Please note: this also seems to address the following warning:

    WordPress database error: [Unknown character set: 'utf']
    set names utf-8
  2. mitcho (Michael Yoshitaka Erlewine)
    Member
    Plugin Author

    Posted 3 years ago #

    @robbyslaughter I'm unable to reproduce this issue. You say $post may not be an object "if for example you are viewing a regular WordPress page". Two things: WordPress posts when displayed also use the global $post variable. It should always be an object. Second, if you are displaying related posts on a page, you probably did so by calling a function such as related_pages() manually in your theme, not using the yarpp_default filter.

    How are you displaying related posts? Via the "automatically display" option, or by adding code to your theme? Do you only get this warning on pages but not posts?

  3. robbyslaughter
    Member
    Posted 3 years ago #

    First: it's just a notice, not a warning or an error. It only appears on my site if I change WP_DEBUG to true. I'm not suggesting that this is in any way critical, I'm just documenting this issue in case someone else happens along it later.

    I don't believe the notice solely connected to YARPP. I am using the Improved Include page plugin, and it appears each time a page is "included" with this plugin. I think when Improved Include executes, it does not create a $post variable in the same way as part of the regular loop. My perception that the issue was tied to regular WordPress pages was in fact a red herring; it seems to only occur with use of Improved Include.

    In any case, we are not using the automatic insertion feature of YARPP. Instead, we are in fact calling related_posts(); from single.php.

    Since the function yarpp_default gets added as a filter to the_content, it seems like there are cases in which this function could be called outside of The Loop. So, it would seem like better coding not to assume $post exists and instead to check as I have suggested.

  4. mitcho (Michael Yoshitaka Erlewine)
    Member
    Plugin Author

    Posted 3 years ago #

    Well, the issue then must be that the_content is be getting called outside of the Loop, which, I believe, is not supported by WordPress.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

  • RSS feed for this topic
  • Started 3 years ago by robbyslaughter
  • Latest reply from mitcho (Michael Yoshitaka Erlewine)
  • This topic is not a support question
  • WordPress version: 3.1