1

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.

  • wp_count_posts sounds like it's exactly what you're looking for. You will still have to limit the result for the post statuses you want, but it's returning the status and post count for your user. – MikeNGarrett Apr 16 at 15:01
  • @MikeNGarrett well, perhaps I am doing something wrong.. – samjco Apr 16 at 18:06
  • Try wp_count_posts( 'page' ); – MikeNGarrett Apr 16 at 18:10
  • @MikeNGarrettI have updated the code, and it doesn't fully count correctly. I've added 2 authors with different assign statuses and in one of the authors view, I see just the max total of both authors of post per status.. Strange. Is there anyway to force wp_count_posts( ) to an author? – samjco Apr 17 at 3:05
  • I'm wondering if I might need to take a jQuery approach to this? – samjco Apr 17 at 3:20
0

Ah! jQuery to the rescue!

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);

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.