in my plugin I need to retrieve posts published by admins to display them in the back-end under posts so .. i fetch the admins ids through get_users and it's fine but whenever i try to build a query and pass these ids as author ids to get what these authors' posts my query keeps looping infinitely , here's the function :
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);
and it doesn't change the fact that it loops infinitely even if i put this :
if ($myposts -> have_posts())
{
while ($myposts -> have_posts() )
{
$myposts -> the_post();
echo $post->ID, '<br />';
} // end while
} // end if
else
{
echo "no posts";
}
}
}
Is my query causing the problem ?cause I've tried a few but they always loop infinitely and I try to keep it simple ..please help So I tried a new function that retrieves post ids of posts published by admins only problem is i cant get posts to show in the table in posts ..how can i do that ? here's the function
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}\"%'" );
}
I call it in this function:
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');
the $query->set shows no posts ! as for new WP_Query it shows all posts ..admin or not ! what am I missing?
$posts
for your query, as this is also used in core functions...