web-dev-qa-db-fra.com

Afficher uniquement le nombre de types de publication personnalisé pour l'auteur actuel dans le widget de tableau de bord "En bref"

J'utilise ce code pour afficher tous les types d'articles personnalisés avec leur nombre dans le widget de tableau de bord "En bref":

add_action( 'dashboard_glance_items', 'cpad_at_glance_content_table_end' );
function cpad_at_glance_content_table_end() {
$args = array(
    'public' => true,
    '_builtin' => false,
);
$output = 'object';
$operator = 'and';

$post_types = get_post_types( $args, $output, $operator );
foreach ( $post_types as $post_type ) {
    $num_posts = wp_count_posts( $post_type->name );
    $num = number_format_i18n( $num_posts->publish );
    $text = _n( $post_type->labels->singular_name, $post_type->labels->name, intval( $num_posts->publish ) );
    if ( current_user_can( 'edit_posts' ) ) {
        $output = '<a href="edit.php?post_type=' . $post_type->name . '">' . $num . ' ' . $text . '</a>';
        echo '<li class="post-count ' . $post_type->name . '-count">' . $output . '</li>';
        } else {
        $output = '<span>' . $num . ' ' . $text . '</span>';
            echo '<li class="post-count ' . $post_type->name . '-count">' . $output . '</li>';
        }
    }
}

Existe-t-il un moyen d'afficher uniquement le nombre de publications appartenant à l'auteur actuellement connecté?

2
quantum_leap

Je pense que ce que vous recherchez, c'est:

   // The user is logged, retrieve the user id (this needs to go above your foreach)
    $user_ID = get_current_user_id(); 
   // Now we have got the user's id, we can pass it to the foreach of your function(this needs to go into your foreach:  
    echo '<li>Number of '.$post_type->name.' posts published by me: ' . count_user_posts( $user_ID , $post_type->name ).'</li>';
2
rudtek

J'ai modifié la réponse de @rudtek et j'affiche le code ici. Fondamentalement, la fonction count_user_posts avait besoin de la méthode name transmise au type de publication, sinon la sortie était "0".

add_action( 'dashboard_glance_items', 'cpad_at_glance_content_table_end' );
function cpad_at_glance_content_table_end() {
$args = array(
    'public' => true,
    '_builtin' => false,
);
$output = 'object';
$operator = 'and';

$post_types = get_post_types( $args, $output, $operator );
foreach ( $post_types as $post_type ) {
    $user_ID = get_current_user_id();
    $num_posts = count_user_posts( $user_ID , $post_type->name );
    $text = _n( $post_type->labels->singular_name, $post_type->labels->name, intval( $num_posts->publish ) );
    if ( current_user_can( 'edit_posts' ) ) {

        $output = '<a href="edit.php?post_type=' . $post_type->name . '">' . $num_posts . ' ' . $text . '</a>';
        echo '<li class="post-count ' . $post_type->name . '-count">' . $output . '</li>';
        } else {
        $output = '<span>' . $num_posts . ' ' . $text . '</span>';
            echo '<li class="post-count ' . $post_type->name . '-count">' . $output . '</li>';
        }
    }
}
0
quantum_leap