WordPress新手求助:排查PHP Notice: Trying to get property of non-object错误
Hey there! I totally get how frustrating that notice can be when you're starting out with PHP and WordPress—let's break down exactly what's causing it and how to fix it, since you've narrowed the issue down to your bee_security function hooked to template_redirect.
What's the root cause?
This error pops up when your code tries to access a property (using the -> syntax) on something that isn't an object. Most often in WordPress, this happens because:
- You're assuming a global variable (like
$post) exists and is an object, but it's actuallynull(e.g., on a 404 page, archive, or homepage where there's no single post/page object) - You're using a function that might return
falseornullinstead of an object (likeget_user_by()if the user doesn't exist, orget_post()for an invalid post ID) - The object you're trying to access hasn't been fully initialized yet when the
template_redirecthook runs
Common fixes tailored to your code
Let's walk through examples of how to fix the most likely issues in your bee_security function:
1. Checking if the global $post object exists before using it
If your code is trying to check the current page ID or post type, don't directly use $post->ID without verifying the object exists first.
Bad code (causes the notice):
function bee_security() { global $post; // No check if $post is actually an object if ( $post->ID != 123 ) { wp_redirect( home_url() ); exit; } } add_action('template_redirect', 'bee_security');
Fixed code:
function bee_security() { global $post; // First confirm $post is an object and has the ID property if ( isset( $post ) && is_object( $post ) && $post->ID != 123 ) { wp_redirect( home_url() ); exit; } // Even better: Use WordPress built-in conditionals which handle checks for you if ( !is_page( 123 ) ) { wp_redirect( home_url() ); exit; } } add_action('template_redirect', 'bee_security');
2. Verifying function returns are objects before accessing properties
If your code uses functions like get_user_by() or similar that might return non-objects, always check the result first.
Bad code:
function bee_security() { $current_user = wp_get_current_user(); // No check if user data is valid if ( $current_user->user_role != 'subscriber' ) { wp_redirect( home_url() ); exit; } }
Fixed code:
function bee_security() { $current_user = wp_get_current_user(); // Check if we have a valid user object and the role property exists if ( is_object( $current_user ) && property_exists( $current_user, 'roles' ) && !in_array( 'subscriber', $current_user->roles ) ) { wp_redirect( home_url() ); exit; } }
3. Using more reliable WordPress functions
Instead of relying on global variables, use get_queried_object() which returns the current queried object (post, page, category, etc.) or null if none exists. It's often more reliable than the global $post in hooks like template_redirect:
function bee_security() { $queried_obj = get_queried_object(); // Check if it's a page object before accessing its ID if ( is_object( $queried_obj ) && $queried_obj->post_type === 'page' && $queried_obj->ID != 123 ) { wp_redirect( home_url() ); exit; } }
Quick troubleshooting steps for your specific code
- Go through every line in your
bee_securityfunction where you use->to access a property - For each one, add a check to confirm the variable is an object first (use
is_object($var)andisset($var->property)to be safe) - Replace direct object property checks with WordPress built-in conditionals (like
is_page(),is_single(),is_user_logged_in()) whenever possible—these functions already handle existence checks under the hood
That should squash that notice for you! If you can share the full code of bee_security, I can give even more targeted advice.
内容的提问来源于stack exchange,提问作者Aaron McKeon

