获取使用PRE_GET_POSTS查询的每个状态的帖子的.subsubsubcount

时间:2019-04-15 作者:samjco

My following query using pre_get_posts shows the Author only certain posts or pages with certain statuses that s/he should only see and of what belongs to him/her.

Is there any way I can get a count of each item shown based on status that renders in my WP List Table view (aka admin post table)?

I am trying to recreate my own version of the .subsubsub links and only show the numbers of the posts "in views" broken down by status, because the current WP default system does not count correctly when $query->set() is added in to change the default list views.

For example. If the query show that the current user has a total of 10 posts assigned, it should show like:

All (10) | Published (4) | Private (0) | Draft (0) | Assigned (6)

Even with my query working, it still shows ALL posts, all published, etc.

Here is my current code:



function posts_for_current_author($query) {

    require_once(ABSPATH . \'wp-admin/includes/screen.php\');
    global $pagenow,
           $get_all_statuses;   

           $get_all_statuses =  \'publish\', \'private\', \'draft\', \'status-assigned\';  
           $get_all_statuses = str_replace(" ","", $get_all_statuses); // clear any spacing
           $get_all_statuses = explode(",", $get_all_statuses);          

    $current_user = wp_get_current_user();   
    $current_screen = get_current_screen();

    if( \'edit.php\' != $pagenow || \'page\' != $current_screen->post_type || !$query->is_admin )
       return $query;

  if(current_user_can(\'author\')):

    $query->set(\'author\', $current_user->ID  );
    $query->set( \'post_status\', $get_all_statuses );

  endif;

  return $query;

}  
add_filter(\'pre_get_posts\', \'posts_for_current_author\'); 


// Function to update the subsubsub links with new values
add_filter(\'views_edit-page\',\'wp37_update_page_quicklinks\',1);

function wp37_update_page_quicklinks($views) { 

    global $get_all_statuses; // status to show 

    $remove_statuses_out_array = array(\'pending\',\'scheduled\', status-open);  //status to remove

    //Get total count for ALL POST
    $stat_count = wp_count_posts( \'page\' );

    // Show ALL as first link
    $views[\'all\'] = sprintf(__(\'<a href="%s" title="All Post">All <span class="count">(%d)</span></a>\', \'all\'),  admin_url(\'edit.php?post_type=page\'), $stat_count);  
    foreach($get_all_statuses as $status_label => $status_name):

    //echo  "<br>".$status_name.": ".$stat_count->$status_name;
      $views[$status_name] = sprintf(__(\'<a href="%s" title="\'.$status_label.\'">\'.$status_label.\'<span class="count">(%d)</span></a>\', $status_name),  admin_url(\'edit.php?post_type=page&post_status=\'.$status_name), $stat_count->$status_name);  

    endforeach;

    foreach( $remove_statuses_out_array as $rem ):
      unset($views[$rem]); 
    endforeach;

    return $views;  
}  



I have tried anything I could think of, like:

$count_pages = wp_count_posts( $post_type = \'page\', $perm = "" );

Or I\'ve looked into this filter views_{$this->screen->id}:

$views = apply_filters( "views_edit-page", $views );

But nothing seems to work. So I was wondering if there is a count query param somewhere. My last resort would be, I guess, to go into the wpdb.

1 个回复
最合适的回答,由SO网友:samjco 整理而成

啊!jQuery救援!

function custom_admin_js() {

      global $get_all_statuses;  ?>

      <script type="text/javascript"> 
        jQuery(document).ready(function($) {
          //Bring my $get_all_statuses array and encode it for jquery 
          var myObjects = <?php echo json_encode($get_all_statuses);?>;

          //Run a each loop : jquery equivalent to php\'s foreach
          $.each(myObjects, function (index, value) {

            //console.log(index); //Status Label
            //console.log(index); //Status-name

            //count the WP list rows that has class names that matches your status name
            // For example: <div class="status-col status-col-Published">Published</div>
            var statct = $(".status-col-"+index).length,
                allct = $(".status-col").length; // Target the class that is unique to count all rows.

            //Update the value to the subsubsub element!
            $(\'.subsubsub li.\'+value+\' .count\').html(statct);
            $(\'.subsubsub li.all .count\').html(allct);

          });
        }); 
        </script>
    <?php
}

add_action(\'admin_footer\', \'custom_admin_js\', 1);