欢迎使用WPSE。您要防止的条件是ID为0,因此仅在不允许的情况下运行查询。
此外,请保持干燥-不要重复。而不是打电话get_current_user_id()
多次调用一次并存储它。
$reg_count= 0;
$user_id = get_current_user_id();
if ( 0 !== $user_id ) {
echo "looking for registrations with author id of $user_id"; //PHP will output var value in double quotes
$registrations = new WP_Query(
array(
\'posts_per_page\' => -1,
\'post_type\' => "registration",
\'author\' => $user_id,
\'meta_key\' => \'first_name\',
\'orderby\' => \'meta_value\',
\'order\' => \'ASC\',
\'meta_query\' => array(
array(
\'key\' => \'event_id\',
\'compare\' => \'LIKE\',
\'value\' => \'"\' . get_the_id() . \'"\' //Why not just get_the_id()?
),
),
));
echo "found $registrations->found_posts registrations with author id of $user_id";
}
注意:不在我的办公桌上-上面的代码未经测试。如果有问题,请在此处进行评论。
编辑:添加了缺少的右括号IF
陈述