Чтобы подсчитать пользовательские типы сообщений в зависимости от автора в WordPress, вы можете использовать различные методы. Вот несколько примеров кода:
-
Использование WP_Query:
$args = array( 'post_type' => 'your_custom_post_type', 'author' => $author_id, // Replace $author_id with the ID of the author 'posts_per_page' => -1, // Retrieve all posts ); $query = new WP_Query($args); $count = $query->post_count;
-
Использование get_posts:
$args = array( 'post_type' => 'your_custom_post_type', 'author' => $author_id, // Replace $author_id with the ID of the author 'posts_per_page' => -1, // Retrieve all posts ); $posts = get_posts($args); $count = count($posts);
-
Использование специального SQL-запроса:
global $wpdb; $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'your_custom_post_type' AND post_author = %d", $author_id // Replace $author_id with the ID of the author ) );
Это всего лишь несколько примеров. Есть и другие способы добиться того же результата. Выберите метод, который лучше всего соответствует вашим потребностям и стилю кодирования.