我的查询一直在无限循环!怎样才能阻止它?

时间:2016-12-19 作者:Hind

在我的插件中,我需要检索管理员发布的帖子,以便在后端的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查询,它显示所有帖子。。管理与否!我错过了什么?

1 个回复
SO网友:Tunji

就像Tom评论的那样,您的代码很难阅读。您可以查看下面的代码片段,作为基于代码内容的建议。

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";
        }
    }
}
首先,我做了一些更正

您没有申报$paged 变量

  • $admins_ids 应该是array() 如果你想用它author__inWP_query 变量来自$posts$loop