admin管理员组

文章数量:1287117

How to display the number of articles published per user excluding users who do not publish articles. My message type is recipe. I would like to receive a code to get there in wp-query thank you very much for your help

I give you a piece of code. Thanks to this code I am able to display the total number of recipes, but I would have liked to have this: User A posted 14 cooking recipes, User B posted 2 cooking recipes ect.. user member1 should have 2 recipes

<?php
// 1. We define the arguments to define what we want to recover 
$args = array ( 'post_type' => 'recipe', 'posts_per_page' => '16', );

// 2. We run the WP Query 
// The Query 
$the_query = new WP_Query ($args);

// 3. we display the number of messages and the authors!
 // The Loop 
if ($the_query-> have_posts ()) {
 echo count_user_posts (2, $args); 
echo 'recipes for'; 
echo get_the_author (2, $args); 
echo '<br>'; 
echo count_user_posts (1, $args); 
echo 'recipes for';
 echo get_the_author (1, $args);

// 3. We launch the loop to display the articles and the authors! 
// The Loop 
echo '<ul>'; 
while ($the_query-> have_posts ()) ​{ 
$the_query-> the_post (); 
echo '<li>'. get_the_title (). '<li>'; 
echo '<li>'. get_the_author (). '<li>';
​}
​echo '<ul>';
​}
​else { 
// no posts found }
/ * Restore original Post Data * / 
wp_reset_postdata ();
​?>

        

    

How to display the number of articles published per user excluding users who do not publish articles. My message type is recipe. I would like to receive a code to get there in wp-query thank you very much for your help

I give you a piece of code. Thanks to this code I am able to display the total number of recipes, but I would have liked to have this: User A posted 14 cooking recipes, User B posted 2 cooking recipes ect.. user member1 should have 2 recipes

<?php
// 1. We define the arguments to define what we want to recover 
$args = array ( 'post_type' => 'recipe', 'posts_per_page' => '16', );

// 2. We run the WP Query 
// The Query 
$the_query = new WP_Query ($args);

// 3. we display the number of messages and the authors!
 // The Loop 
if ($the_query-> have_posts ()) {
 echo count_user_posts (2, $args); 
echo 'recipes for'; 
echo get_the_author (2, $args); 
echo '<br>'; 
echo count_user_posts (1, $args); 
echo 'recipes for';
 echo get_the_author (1, $args);

// 3. We launch the loop to display the articles and the authors! 
// The Loop 
echo '<ul>'; 
while ($the_query-> have_posts ()) ​{ 
$the_query-> the_post (); 
echo '<li>'. get_the_title (). '<li>'; 
echo '<li>'. get_the_author (). '<li>';
​}
​echo '<ul>';
​}
​else { 
// no posts found }
/ * Restore original Post Data * / 
wp_reset_postdata ();
​?>

        

    

Share Improve this question edited Sep 27, 2021 at 20:45 Pat J 12.4k2 gold badges28 silver badges36 bronze badges asked Sep 27, 2021 at 10:10 flexi2202flexi2202 175 bronze badges 4
  • 1 Display the number where? – Jacob Peattie Commented Sep 27, 2021 at 10:14
  • thank you for the answer. on a simple php page for the moment I manage to display all the articles of all the users with this code <?php $args = array( 'post_type' => 'recette' ); $my_query = new WP_Query( $args ); echo $my_query->found_posts . " articles trouvés"; ?> – flexi2202 Commented Sep 27, 2021 at 11:43
  • Please edit your question if you want to provide updates, code snippets, etc. – Pat J Commented Sep 27, 2021 at 13:36
  • 1 Here I have modified I'm looking for a code – flexi2202 Commented Sep 27, 2021 at 14:30
Add a comment  | 

2 Answers 2

Reset to default 0

If you have access to user IDs, you can use the count_user_posts() function.

You would get the number of posts by a user like this:

//Assume the variable $thisUser is equal to a valid user ID
$ThisUserCount = count_user_posts($thisUser, 'recipe');

EDIT: Where you're calling the count_user_posts() function, you're passing it the array $args instead of the post type, 'recipe'.

<?php
// 1. We define the arguments to define what we want to recover
$args = array (
    'post_type' => 'recipe',
    'posts_per_page' => '16',
);

// 2. We run the WP Query
// The Query
$the_query = new WP_Query ($args);

// 3. we display the number of messages and the authors!
// The Loop
if ($the_query-> have_posts()) {
    //Set arguments to grab all authors with published recipes, and order them by user ID
    $authorArgs = array(
        'orderby' => 'ID',
        'has_published_posts' => array('recipe'),
    );

    //Create an array of all authors with recipes published
    $recipeAuthors = get_users($authorArgs);

    //Loop through each recipe author
    foreach($recipeAuthors as $user){
        //Output user post count for recipes
        echo count_user_posts($user->ID, 'recipe');
        echo ' recipes for ';

        //Output user's display name
        echo $user->display_name;
        echo '<br />';
    }

    // 3. We launch the loop to display the articles and the authors!
    // The Loop
    echo '<ul>';
    while ($the_query-> have_posts()) {
        $the_query-> the_post();
        echo '<li>'. get_the_title(). '</li>';
        echo '<li>'. get_the_author(). '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}
wp_reset_postdata ();​?>

Also, your get_the_author(2,$args) function calls are not correct. get_the_author() does not accept any parameters anymore, and it only returns the Display Name of the author of the current post in the Loop.

If you're just looking to display authors who have posted at least one article in your custom post type you can ignore the above code and just do this:

// Array of WP_User objects.
$authors = get_users();

// Loop thru the array and get the post count for each user
foreach ( $authors as $author ) {
    $posts = count_user_posts($author->ID, 'movies');

    // Only return users who have at least one post 
    if ($posts > 0):
        echo '<p><span>' . esc_html( $author->display_name ) . ': ' . $posts . '</span>';
    endif; 
}

// Only Necessary if you're running another loop or query on the page
wp_reset_postdata ();​

Good luck!

本文标签: wp queryHow to display the number of articles published per user