当我尝试在wordpress中获取自定义查询的帖子数时,输出以下错误。
Warning: array_map(): Argument #2 should be an array in /Applications/XAMPP/xamppfiles/htdocs/wordpress/wp-includes/class-wp-query.php on line 1918
Warning: implode(): Invalid arguments passed in /Applications/XAMPP/xamppfiles/htdocs/wordpress/wp-includes/class-wp-query.php on line 1918
0
下面是我的功能:
if( !function_exists('my_likes') ){
function my_likes() {
global $current_user;
$current_user = get_current_user_id();
// The Query
$args = array(
'meta_key' =>'likes_count',
'orderby' => 'date',
'post__in' => $current_user
);
$obj_name = new WP_Query($args);
$num = $obj_name->post_count;
print $num;
}
add_action('my_likes', 'my_likes');
}
答案 0 :(得分:0)
Function
这是一种计数用户帖子的效率低下的方法,因为它获取了所有帖子对象。也许只是使用原始SQL查询
答案 1 :(得分:0)
'post__in' => $current_user
行是错误的,post__in应该是一个数组,因此,如果您要像这样使用它,就需要'post__in' => array($current_user)
从上下文中也不能确定您是否要使用此行,如果要计算由$ current_user标识的用户发布的帖子,则应该使用'post__in' => $current_user
而不是'author' => $current_user
顺便说一句。完全不需要global $current_user;
行,并且覆盖了全局$ current_user值。
我认为完整的代码应该像这样
function my_likes() {
$current_user = get_current_user_id();
// The Query
$args = array(
'meta_key' =>'likes_count',
'orderby' => 'date',
'post__in' => array($current_user),
// or 'author' => $current_user
);
$obj_name = new WP_Query($args);
$num = $obj_name->post_count;
print $num;
}