admin管理员组文章数量:1431912
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
.
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 Answer
Reset to default 0Ah! 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);
本文标签: filtersGet subsubsub count of post per status queried using pregetposts
版权声明:本文标题:filters - Get .subsubsub count of post per status queried using pre_get_posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745581216a2664634.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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 Commented Apr 16, 2019 at 15:01wp_count_posts( 'page' );
– MikeNGarrett Commented Apr 16, 2019 at 18:10