首页 > 解决方案 > 识别哪个项目是非对象(PHP 注意:尝试获取非对象的属性)

问题描述

我对 PHP 和 WordPress 比较陌生,这条错误消息“PHP 通知:尝试获取非对象的属性”一直困扰着我,我相信有一个简单的解决方法。任何人都可以扫描以下代码并让我知道此错误的根源是什么?我确信我已将其范围缩小到这段代码。提前感谢您的帮助!

// REDIRECT USERS IF THEY ARE IN THE WRONG SPOT
add_action ('template_redirect', 'bee_security');
function bee_security() {

    // set the page that people end up on if they try to access the wrong page
    $bee_redirecturl = '/private-page/home/';

    // get basic user information
    $bee_masteruserid = get_user_meta(get_current_user_id(), 'wpcf-user-masteruser', true);
    $bee_temppost = get_post($post = get_the_id());
    $bee_authorid = $bee_temppost->post_author;

    // determine if the current post type is related to households
    $bee_posttype_household = substr(get_post_type(), 0, 9);
    if ( $bee_posttype_household == "household") { 
        $bee_posttype_household = true; 
    } else { 
        $bee_posttype_household = false; 
    }

    // redirect the user if they are logged in and accessing the front page
    if ( is_front_page() && is_user_logged_in() ) {
        wp_redirect($bee_redirecturl);
        exit;

    // redirect the user if they try to access somebody else's househould besides their own
    } elseif ( $bee_posttype_household == true ) {
        if ( $bee_authorid != get_current_user_id() && $bee_authorid != $bee_masteruserid ) {
          wp_redirect($bee_redirecturl);
          exit;
        } 
    // redirect the user if they try to make a review on someone else's behalf
    } elseif ( get_the_id() == 347 ) {
        $bee_childpost = get_post($_GET['childid']);
        $bee_childauthor = $bee_childpost->post_author;
        if ( $bee_childauthor != get_current_user_id() && $bee_childauthor != $bee_masteruserid ) {
            wp_redirect($bee_redirecturl); 
        }
    }
}

标签: phpwordpress

解决方案


以下值之一产生错误

$bee_authorid = $bee_temppost->post_author;

或者

$bee_childauthor = $bee_childpost->post_author;

get_post()不返回值,这可能是原因


推荐阅读