Arrays 在另一个数组中使用一个数组的结果
我有一个登录用户在博客中跟随其他用户。我想让我的登录用户看到他们关注的用户的帖子 我运行一个查询,返回一个用户ID(用户ID 1)列表,登录用户(用户ID 2)在数组中跟随该列表。然后,我通过foreach循环运行该数组,以获取作为用户ID的用户列表,并将其放置在一个新数组中。返回的user1_id作为字符串进行回显。我现在想在另一个数组中使用返回的user1\u id,该数组只显示基于user1\u id的帖子 问题是,这些值是作为字符串返回的,而我在POST中使用的第二个数组只读取第一个数组的第一个数字,因为这些值是作为字符串而不是整数返回的 如何使用第二个数组“author\uu in'=>array()中的第一个数组的结果?我是否需要将字符串转换为整数,还是有更好的方法Arrays 在另一个数组中使用一个数组的结果,arrays,wordpress,post,Arrays,Wordpress,Post,我有一个登录用户在博客中跟随其他用户。我想让我的登录用户看到他们关注的用户的帖子 我运行一个查询,返回一个用户ID(用户ID 1)列表,登录用户(用户ID 2)在数组中跟随该列表。然后,我通过foreach循环运行该数组,以获取作为用户ID的用户列表,并将其放置在一个新数组中。返回的user1_id作为字符串进行回显。我现在想在另一个数组中使用返回的user1\u id,该数组只显示基于user1\u id的帖子 问题是,这些值是作为字符串返回的,而我在POST中使用的第二个数组只读取第一个数组
// The database query that returns the array
<?php
$currentloggedinuser = get_current_user_id();
$followers_query = $wpdb->get_results("SELECT ID, user_id1 FROM wp_followers WHERE user_id2 = '$currentloggedinuser' ") ?>
// the returned array from the query through foreach placed in another array
<?php
$following_id = array();
foreach ($followers_query as $follower) {
$following_id[] = $follower->user_id1;
sort($following_id);
$following_ids = implode(", ",$following_id);
}
?>
// the resulting number values returned as a string
<?php echo $following_ids; ?>
<?php
$args = array(
'author__in'=> array($following_ids), // user1_ids I'd like to include from the array above
'post_type' => 'post'
);
//返回数组的数据库查询
这可能会奏效。我没有测试它,但它应该是好的
这里是一些关于
只要做'author\uu in'=>$following\u id
-这样你就可以在爆炸前使用数组了谢谢:)我试过了,效果不错。谢谢你的链接,并提醒我前缀和get_col。有时我真不敢相信stackoverflow上的人有多棒:D有一天很棒:)哈哈,你让我开心:)很高兴我能帮上忙:)如果答案对你有帮助,请确保你将其标记为解决方案:)谢谢,祝你过得愉快!
<?php
$currentloggedinuser = get_current_user_id();
// Get an array containing only the user_id1 values.
// It's a good habit to use the prepare method of $wpdb
// for security and ease of reading the code
// Also, you should use $wpdb->prefix instead of using the 'wp_' for the table name
// that prefix can be changed and your code would break.
$followers = $wpdb->get_col(
$wpdb->prepare(
"SELECT user_id1 FROM {$wpdb->prefix}followers WHERE user_id2 = %d",
$currentloggedinuser
)
);
sort($followers); // or you can just apply sorting in the query above
// Directly echo the imploded array.
// No need to store it in a variable to do so unless you want to use it somewhere else
echo implode(', ', $followers);
$args = array(
'author__in'=> $followers, //use the array we got from the query
'post_type' => 'post'
);