比较 WordPress 中的用户名和自定义字段

comparing username and custom field in WordPress

我正在尝试根据登录用户和自定义字段值显示消息。 如果登录用户和自定义字段值相同,则应显示 "this page is assigned for you"

否则,应该显示"this page is NOT assigned for you"

我的代码是这样的:

global $wp_query;
$postid = $wp_query->post->ID;
$getClient = get_post_meta($postid, 'clientName', true);
echo 'Client is : '.$getClient;

$current_user = wp_get_current_user();
echo '<br/>Username: ' . $current_user->user_login . '<br />';

if ($getClient !== $current_user){
    echo 'this page is NOT assigned for you';
}
else {
    echo 'this page is assigned for you';
}

当我转到页面时,如果用户名和自定义字段值相同,则显示:

Client is : cd_riffaz
Username: cd_riffaz
this page is NOT assigned for you

实际上它应该显示“this page is assigned for you”对吗?因为值相同。

为什么它没有按预期工作?

您正在比较对象 ($current_user) 和字符串 $getClient
你应该做 if ($getClient !== $current_user->user_login)

P.S。你可以做 global $current_user; 而不是 $current_user = wp_get_current_user();