/** * Correct Author Post Counts with Polylang Support * Works for ALL post types + ALL languages (including lang=all) */ if (!defined('ABSPATH')) { exit; } function najibul_correct_author_post_counts($views, $post_type) { // Only apply to non-admin users if (current_user_can('administrator')) { return $views; } $user_id = get_current_user_id(); // Detect language $current_lang = isset($_GET['lang']) ? sanitize_text_field($_GET['lang']) : ''; // If no Polylang or language detected, fallback to ALL languages $is_all_languages = ($current_lang === '' || $current_lang === 'all'); // Base query args $base_args = [ 'post_type' => $post_type, 'author' => $user_id, 'post_status' => ['publish', 'draft', 'trash'], 'posts_per_page' => -1, 'fields' => 'ids' ]; // Apply Polylang language filter only if specific language selected if (!$is_all_languages && function_exists('pll_current_language')) { $base_args['lang'] = $current_lang; } // Fetch all posts of current user $all_posts = get_posts($base_args); // Categorize counts $published = 0; $drafts = 0; $trash = 0; foreach ($all_posts as $post_id) { $status = get_post_status($post_id); if ($status === 'publish') $published++; if ($status === 'draft') $drafts++; if ($status === 'trash') $trash++; } // Total count $total = count($all_posts); // Rebuild Views if (isset($views['all'])) { $views['all'] = preg_replace('/\(\d+\)/', '(' . $total . ')', $views['all']); } if (isset($views['mine'])) { $views['mine'] = preg_replace('/\(\d+\)/', '(' . $total . ')', $views['mine']); } if (isset($views['publish'])) { $views['publish'] = preg_replace('/\(\d+\)/', '(' . $published . ')', $views['publish']); } if (isset($views['draft'])) { $views['draft'] = preg_replace('/\(\d+\)/', '(' . $drafts . ')', $views['draft']); } if (isset($views['trash'])) { $views['trash'] = preg_replace('/\(\d+\)/', '(' . $trash . ')', $views['trash']); } return $views; } // Apply to all post types you use $post_types = ['post', 'news', 'tools', 'qa']; foreach ($post_types as $pt) { add_filter("views_edit-$pt", function ($views) use ($pt) { return najibul_correct_author_post_counts($views, $pt); }); }