My query keeps looping infinitely ! how to stop it?

Like Tom commented, your code is hard to read. You can look at the code snippet below as a suggestion based on what your code.

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.
        $admin_ids = array();
        foreach ( $Admins as $post_author ) {
            $admin_ids[] = $post_author->ID;
        }

        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
        $args2 = array('author__in' => $admin_ids, 'posts_per_page' => 12, 'paged' => $paged);

        $loop = new WP_Query($args2);

        if ( $loop->have_posts() ) {
            while ( $loop->have_posts() ) : $loop->the_post();

                echo get_the_ID(), '<br />';
            endwhile; // end while
        }  else {
            echo "no posts";
        }
    }
}

I’ve made some corrections, first of all

  • You didn’t declare the $paged variable.
  • $admins_ids should be an array() if you’re going to use it for author__in
  • I’ve also changed the WP_query variable from $posts to $loop