Skip to main content
updated answer from new information
Source Link
Joe
  • 204
  • 5
  • 13
<?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 THISarguments LINEto WILLgrab ECHOall HOWauthors MANYwith 'recipe'published POSTSrecipes, USERand IDorder #2them HASby user ID
    echo$authorArgs count_user_posts(2,= 'recipe');array(
    echo 'recipes for';  'orderby' => 'ID',
    $user = get_user_by( 'ID' 'has_published_posts' => array('recipe'), 
 2   ); 

    echo//Create $user->display_name;an array of all authors with recipes published
    echo$recipeAuthors '<br>';= get_users($authorArgs);

    //Loop THISthrough LINEeach WILLrecipe ECHOauthor
 HOW MANY 'recipe' POSTSforeach($recipeAuthors USERas ID$user){
 #1 HAS      //Output user post count for recipes
        echo count_user_posts(1$user->ID, 'recipe');
        echo 'recipes' for';recipes for ';

    $user = get_user_by( 'ID', 1//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 ();​?>
<?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()) {
    // THIS LINE WILL ECHO HOW MANY 'recipe' POSTS USER ID #2 HAS
    echo count_user_posts(2, 'recipe');
    echo 'recipes for';
    $user = get_user_by( 'ID', 2 );
    echo $user->display_name;
    echo '<br>';
    // THIS LINE WILL ECHO HOW MANY 'recipe' POSTS USER ID #1 HAS
    echo count_user_posts(1, 'recipe');
    echo 'recipes for';
    $user = get_user_by( 'ID', 1 );
    echo $user->display_name;

    // 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 ();​?>
<?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 ();​?>
added 72 characters in body
Source Link
Joe
  • 204
  • 5
  • 13

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()) {
    // THIS LINE WILL ECHO HOW MANY 'recipe' POSTS USER ID #2 HAS
    echo count_user_posts(2, 'recipe');
    echo 'recipes for';
    echo$user get_the_author= get_user_by(2 'ID', $args2 );
    echo $user->display_name;
    echo '<br>';
    // THIS LINE WILL ECHO HOW MANY 'recipe' POSTS USER ID #1 HAS
    echo count_user_posts(1, 'recipe');
    echo 'recipes for';
    echo$user get_the_author= get_user_by(1 'ID', $args1 );
    echo $user->display_name;

    // 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 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()) {
    // THIS LINE WILL ECHO HOW MANY 'recipe' POSTS USER ID #2 HAS
    echo count_user_posts(2, 'recipe');
    echo 'recipes for';
    echo get_the_author(2, $args);
    echo '<br>';
    // THIS LINE WILL ECHO HOW MANY 'recipe' POSTS USER ID #1 HAS
    echo count_user_posts(1, 'recipe');
    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
}
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 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()) {
    // THIS LINE WILL ECHO HOW MANY 'recipe' POSTS USER ID #2 HAS
    echo count_user_posts(2, 'recipe');
    echo 'recipes for';
    $user = get_user_by( 'ID', 2 );
    echo $user->display_name;
    echo '<br>';
    // THIS LINE WILL ECHO HOW MANY 'recipe' POSTS USER ID #1 HAS
    echo count_user_posts(1, 'recipe');
    echo 'recipes for';
    $user = get_user_by( 'ID', 1 );
    echo $user->display_name;

    // 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.

added 30 characters in body
Source Link
Joe
  • 204
  • 5
  • 13

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()) {
    // THIS LINE WILL ECHO HOW MANY 'recipe' POSTS USER ID #2 HAS
    echo count_user_posts(2, 'recipe');
    echo 'recipes for';
    echo get_the_author(2, $args);
    echo '<br>';
    // THIS LINE WILL ECHO HOW MANY 'recipe' POSTS USER ID #1 HAS
    echo count_user_posts(1, 'recipe');
    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
}
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 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()) {
    // THIS LINE WILL ECHO HOW MANY 'recipe' POSTS USER ID #2 HAS
    echo count_user_posts(2, 'recipe');
    echo 'recipes for';
    echo get_the_author(2, $args);
    echo '<br>';
    // THIS LINE WILL ECHO HOW MANY 'recipe' POSTS USER ID #1 HAS
    echo count_user_posts(1, 'recipe');
    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
}
wp_reset_postdata ();​?>

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()) {
    // THIS LINE WILL ECHO HOW MANY 'recipe' POSTS USER ID #2 HAS
    echo count_user_posts(2, 'recipe');
    echo 'recipes for';
    echo get_the_author(2, $args);
    echo '<br>';
    // THIS LINE WILL ECHO HOW MANY 'recipe' POSTS USER ID #1 HAS
    echo count_user_posts(1, 'recipe');
    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
}
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.

added 30 characters in body
Source Link
Joe
  • 204
  • 5
  • 13
Loading
Edited the answer based on new information.
Source Link
Joe
  • 204
  • 5
  • 13
Loading
Source Link
Joe
  • 204
  • 5
  • 13
Loading