0

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?

  • Your code is difficult to read, can you indent it correctly? – Tom J Nowell Dec 19 '16 at 23:14
  • try not to use $posts for your query, as this is also used in core functions... – Michael Dec 20 '16 at 1:00
  • is this better ? – Hind Dec 21 '16 at 10:53
0

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
  • 1
    Can you explain why it loops infinitely and what specifically stopped it doing so? I would avoid answering in a way that gives a copy paste solution, as the asker doesn't learn anything that way and can run into the same problem again almost immediately – Tom J Nowell Dec 20 '16 at 1:34
  • don't forget the wp_reset_postdata() at the end of the function – Michael Dec 20 '16 at 2:01
  • even with the corrections same result :( – Hind Dec 21 '16 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.