“Attempt to read property … on null” in WordPress: what it means and how to fix it

By CompatNav · Published · Last reviewed · 3 min read

Short answer

It’s a warning: a plugin or theme reads a detail (a property, such as post_title or ID) of something that isn’t there, usually a post, user or term that WordPress didn’t find, so the code got null. The page keeps loading with an empty value. PHP 7 showed it as a notice (“Trying to get property … of non-object”); PHP 8.0 made it a warning. It depends on your content, so only your error log shows it. Update the plugin, or send the message to its developer.

The message, exactly

A theme shows a “related post”, but the post it points to was deleted, so it gets null. On PHP 7.4 that gives a notice; on PHP 8.0 a warning:

wp-content/themes/old-theme/related.php

<?php
function find_related_post() {
    return null; // like get_post() when the post doesn't exist
}

$related = find_related_post();
echo 'Related: ', $related->post_title, "\n";

Output on PHP 7.4.33

Related:
Notice: Trying to get property 'post_title' of non-object in /var/www/html/wp-content/themes/old-theme/related.php on line 7

Output on PHP 8.0.30

Related:
Warning: Attempt to read property "post_title" on null in /var/www/html/wp-content/themes/old-theme/related.php on line 7

The page goes on with an empty title. php.net lists the change for PHP 8.0: “A number of notices have been converted into warnings”, including “Attempting to read a property of a non-object.” (php.net)

Read the message

  • The name in quotes is the detail the code wanted: post_title, ID, name, term_id …
  • The last word is what it got instead: null (nothing found) or bool (usually false, also “nothing found” in WordPress).
  • The path names the plugin or theme: the folder after wp-content/plugins/ or wp-content/themes/, here old-theme.

Why it happens in WordPress

WordPress functions that look something up return “nothing” when it doesn’t exist: for get_post(), the documentation gives the return value as “Type corresponding to $output on success or null on failure.” (developer.wordpress.org) Code that reads ->post_title straight away works as long as the post exists, and warns when it doesn’t: a deleted post, a menu item pointing to a removed page, a user who no longer exists.

Who fixes it

  1. Find the plugin or theme from the path in the message.
  2. Check its settings and widgets for a post, page or user that no longer exists, and choose another one.
  3. Update it (Dashboard → Updates).
  4. Still there? Send one full line of the message to its developer, with what was deleted if you know.

Displayed warnings can break a page’s layout; keep them in a log instead, as shown in the WordPress debug log.

Why no check finds it beforehand

Whether the related post exists depends on your content, not on the code, so a check that reads code can’t predict it. CompatNav doesn’t report it; its scan of the examples on this page finds nothing. The same goes for its sibling “Undefined array key”, the same problem for lists instead of objects.

Key takeaways

  • It’s a warning: the page keeps loading, and the missing detail counts as empty.
  • PHP 7 wrote “Trying to get property … of non-object” as a notice; PHP 8.0 writes “Attempt to read property … on null” as a warning.
  • The name in quotes is the property (post_title, ID, name); the word at the end says what the code got instead (null, bool).
  • It usually means WordPress didn’t find a post, user or term, for example after one was deleted.
  • The fix for developers: check that the object exists before reading from it.

Frequently asked questions

Is it dangerous?

Not by itself: the code carries on with an empty value. But part of the page is probably missing (a title, a link), and displayed warnings can break the layout. Report it to the plugin’s developer.

Why “on null” or “on bool”?

That’s what the code received instead of a post or user. WordPress functions return null or false when they find nothing, for example get_post() for a post that was deleted.

Can I fix it by restoring the deleted post?

Sometimes: if a widget or setting still points to a deleted post, choosing another one in its settings stops the warning. The plugin should still handle the case, so tell its developer too.

Check your own site before you upgrade

CompatNav is a free WordPress plugin. It reads the code of your plugins and themes on your own server and tells you, in plain words, what will break and what will only show notices on the PHP version you choose. It never changes your code. It can’t see problems that only appear while code runs with real data, so a quick check of your site after the upgrade still matters.

Get CompatNav on wordpress.org How it works

Sources

About the code examples: each output is the real output of the code shown, run with the official PHP builds, without a php.ini, with all errors reported and displayed. Only the file path was replaced by a neutral server path.