J'utilise le code ci-dessous pour afficher tous les auteurs et leurs publications (dans un type de publication personnalisé appelé "publication"). Cela fonctionne, mais il répertorie également les utilisateurs qui n'ont pas de messages. Comment puis-je adapter ce code pour ne montrer que les auteurs qui ont posté?
Je vous remercie!
<h1>Articles by Author</h1>
<?php
$allUsers = get_users('orderby=title&order=ASC&post_type=publication');
$users = array();
// Remove subscribers from the list as they won't write any articles
foreach($allUsers as $currentUser)
{
if(!in_array( 'subscriber', $currentUser->roles ))
{
$users[] = $currentUser;
}
}
?>
<?php foreach($users as $user) { ?>
<div class="authorInfo">
<h2 class="authorName"><?php echo $user->display_name; ?></h2>
<?php query_posts( array( 'post_type' => 'publication', 'showposts' => -1, 'author'=>$user->ID ) ); while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
</div>
<?php } ?>
UPDATE: En utilisant la fonction de la réponse ci-dessous et quelques extraits de ce que j'ai lu hier soir, voici ce que j'ai maintenant (ne fonctionne toujours pas comme demandé ci-dessus, mais je voulais vous donner mon code le plus récent):
<?php
// count number of Articles (CPT) written by a user
function count_user_posts_by_type( $userid, $post_type = 'publication' ) {
global $wpdb;
$where = get_posts_by_author_sql( $post_type, true, $userid );
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
return $count;
$post_count = count_user_posts_by_type($userid, $post_type);
$allUsers = get_users('orderby=title&order=ASC&post_type=publication&role=author');
$users = array();
// CONDITIONAL -- IS THIS CORRECT?
if ($post_count > 0 ) {
// Remove subscribers from the list as they won't write any articles
foreach($allUsers as $currentUser)
{
if(!in_array( 'subscriber', $currentUser->roles ))
{
$users[] = $currentUser;
}
}
foreach($users as $user) { ?>
<h2 class="authorName"><?php echo $user->display_name; ?></h2>
<?php query_posts( array( 'post_type' => 'publication', 'showposts' => -1, 'author'=>$user->ID, 'orderby' => 'date', 'order' => 'DESC' ) );
while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php } } ?>
La version par défaut de la fonction WordPress fournit le décompte du type de publication par défaut 'POST' comme indiqué ci-dessous:
<?php
$post_count = count_user_posts($userid);
?>
Pour récupérer le nombre de publications basé sur un type de publication, une fonction personnalisée comme ci-dessous est requise:
<?php
function count_user_posts_by_type( $userid, $post_type = 'post' ) {
global $wpdb;
$where = get_posts_by_author_sql( $post_type, true, $userid );
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
return $count;
}
?>
Utilisation de la fonction ci-dessus:
<?php
$post_count = count_user_posts_by_type($userid, $post_type);
?>
Maintenant que vous avez le nombre de messages, enveloppez votre code dans une boucle conditionnelle.
Comment utiliser dans votre fonction:
<?php
// count number of Articles (CPT) written by a user
function count_user_posts_by_type( $userid, $post_type = 'publication' ) {
global $wpdb;
$where = get_posts_by_author_sql( $post_type, true, $userid );
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
return $count;
}
$allUsers = get_users('orderby=title&order=ASC&post_type=publication');
$users = array();
// Remove subscribers from the list as they won't write any articles
foreach($allUsers as $currentUser)
{
if(!in_array( 'subscriber', $currentUser->roles ))
{
$users[] = $currentUser;
}
}
foreach($users as $user) {
$post_count = count_user_posts_by_type($user->id);
if ($post_count > 0 { ?>
<div class="authorInfo">
<h2 class="authorName"><?php echo $user->display_name; ?></h2>
<?php query_posts( array( 'post_type' => 'publication', 'showposts' => -1, 'author'=>$user->ID ) );
while (have_posts()) : the_post();
the_content();
endwhile; ?>
</div>
<?php }
}
?>