• Hi there!

    I stumbled upon this error while running FeedWordPress under a PHP 8.0.2 environment.

    Such errors are ‘normal’ under PHP 8.0 since it’s much more strict in what can be done with nulls and what cannot (earlier versions would possibly either ignore the error completely or, at worst, throw a warning).

    The culprit seems to be in this file: /wp-content/plugins/feedwordpress/syndicatedpost.class.php at around line 1249:

    
                $old_post = NULL;
                if ($q->have_posts()) :
    		while ($q->have_posts()) : $q->the_post();
                        if (get_post_type($q->post->ID) == $this->post['post_type']):
    

    In PHP versions before 8.0, if $q were null (it would mean that a query made a few lines before had failed), this would work — $q->have_posts() would evaluate to null, and because null is the same as false when comparing the expression within an if, this would work, possibly with a warning, and skip the whole section.

    Under PHP 8.0, things are a bit more tricky: when calling $q->have_posts(), the PHP engine will check if have_posts() is a valid member function of $q. If not, it throws a fatal error. The problem is if $q happens to be null. In that scenario, PHP cannot figure out if the member function is valid or not; it throws a fatal error.

    I’ve encountered this kind of thing over and over again, on non-PHP 8 compliant plugins (and lots of other non-WP code as well). The solution is usually simple:

    
                $old_post = NULL;
                if (!empty($q->have_posts())) :
    		while (!empty($q->have_posts())) : $q->the_post();
                        if (get_post_type($q->post->ID) == $this->post['post_type']):
    

    empty() is a combination of isset() and !is_null(). This will, in general, work to deal with the above error in any circumstances.

    I think I’ve fixed my own error so I hope this reasonably simple fix is available in future versions of FeedWordPress!

    • This topic was modified 3 years, 9 months ago by Gwyneth Llewelyn. Reason: Forgot a line when copying & pasting
  • The topic ‘PHP 8 Fatal error: Call to a member function get__queried_object_id() on null’ is closed to new replies.