在我的插件中,我需要检索管理员发布的帖子,以便在后端的posts so下显示它们。。我通过get\\u users获取管理员ID,这很好,但每当我尝试构建查询并将这些ID作为作者ID传递以获取这些作者的帖子时,我的查询会无限循环,函数如下:
function get_posts_by_admin_role()
{
if(is_admin() && !is_front_page() && !is_singular() && !is_archive()) {
$args =array(
\'role\' => \'administrator\'
\'orderby\' => \'post_count\',
\'order\' => \'ASC\',
\'count_total\' => false
);
$Admins = get_users( $args );
// Array of WP_User objects.
foreach ( $Admins as $post_author )
{
$admin_ids= $post_author -> ID;
echo $post_author -> display_name."<br/>";
/*to see results not only get out of memory or time limit exceeded*/
echo $post_author -> ID;
}
$args2 = array( \'author__in\' => $admin_ids );
$myposts = new WP_Query( $args2);
它并没有改变它无限循环的事实,即使我这样说:
if ($myposts -> have_posts())
{
while ($myposts -> have_posts() )
{
$myposts -> the_post();
echo $post->ID, \'<br />\';
} // end while
} // end if
else
{
echo "no posts";
}
}
}
是我的查询导致了问题吗?因为我试过一些,但它们总是无限循环,我尽量保持简单。。请提供帮助,所以我尝试了一个新功能,可以检索管理员发布的帖子的帖子ID。唯一的问题是我无法在帖子的表格中显示帖子。。我该怎么做?下面是函数
function get_posts_by_author_role($role) {
global $wpdb;
return $wpdb->get_results( "SELECT p.* FROM {$wpdb->posts} p, {$wpdb- >usermeta} u"
." WHERE p.post_type = \'post\'"
." AND p.post_status = \'publish\'"
." AND u.user_id = p.`post_author`"
." AND u.meta_key = \'wp_capabilities\'"
." AND u.meta_value LIKE \'%\\"{$role}\\"%\'" );
}
我在这个函数中调用它:
function filter_posts_list($query)
{
if(is_admin() && !is_front_page() && !is_singular() && !is_archive())
{
$myposts = get_posts_by_admin_role(\'administrator\');
$post_id=array();
foreach($myposts as $post)
{
//echo $post->post_title, \'<br />\';
$post_id[]= $post->ID;
echo $post->ID, \'<br />\';
}
if ( !$query->is_main_query() )
return;
$query->set(\'post__in\' ,array($post_id));
// $query = new WP_Query( \'post__in\' ,array($post_id) );
//return $query;
}
}
add_action(\'pre_get_posts\', \'filter_posts_list\');
$查询->集不显示帖子!对于新的WP\\U查询,它显示所有帖子。。管理与否!我错过了什么?