确定哪个项目是非对象(PHP注意:尝试获取非对象的属性)

时间:2018-04-25 21:25:46

标签: php wordpress

我对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); 
        }
    }
}

2 个答案:

答案 0 :(得分:0)

以下值之一产生错误

$bee_authorid = $bee_temppost->post_author;

OR

$bee_childauthor = $bee_childpost->post_author;

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

答案 1 :(得分:0)

我想我终于明白了。当然,我终于放弃并寻求帮助的那一刻,我弄明白了!我更改了这一行:

$bee_authorid = $bee_temppost->post_author;

对此:

$bee_authorid = get_post_field( 'post_author', get_the_id() );

我想在某些帖子可能没有加载的页面上,该行返回null而不是作为对象。