I have a few needs for my author template page. I have the basis for the codes, although they are not functioning properly. I will award $8 for each working answer.
<strong>1.</strong> Echo join date for user in as M/Y. The following code comes close, but echos the admin join date for all users.
<?php
$register_date = date("m/Y", strtotime(get_the_author_meta('user_registered')));
?>
<p>Member since: </p><?php echo $register_date; ?>
<br />
<strong>2. </strong>Echo number of post each user has made. This is a bit trickier because I'm working with custom posts and I want the number to include both 'review' and 'feature' custom posts. The following code echos just the total number of 'review' posts for all users combined.
<?php
$custom_post_types = wp_count_posts('review','feature');
echo '' . $custom_post_types->publish;
?>
<strong>3. </strong>Echo number of author's pending posts. This also needs to include both 'review' and 'feature' custom posts. I haven't attempted to modify the following code for custom posts, although it doesn't seem to work with regular posts as is since it always displays zero.
<?php global $wpdb;
$query = "SELECT COUNT(*) FROM wp_posts WHERE post_status = 'pending' AND post_author = '$user_id'";
$post_count = $wpdb->get_var($query);
$welcomemessage .= "You currently have $post_count submission(s) pending.";
echo $welcomemessage ?>
<strong>4. </strong>Display author's posts of a specific custom post and taxonomy value. The following works great for just custom posts. How would you add a taxonomy by the name of 'format' with a value of 'lp' into an array?
<?php
$authorID = $curauth->ID; {
$query= 'author=' . $authorID. '&post_type=feature' . $post_type . '&orderby=post_date&order=asc';
query_posts($query);
if ( have_posts() ) : ?> <p>FEATURES</p><hr>
<?php while ( have_posts() ) : the_post(); ?>
<p><?php the_title(); ?><p/>
<?php endwhile; else:
endif; }
wp_reset_query(); ?>
And incase it is relevant, I have the following code on the top of the template:
<?php
get_header();
if(isset($_GET['author_name'])) :
$curauth = get_user_by('slug', $author_name);
else :
$curauth = get_userdata(intval($author));
endif; ?>
Thanks a lot!
Maor Barazany answers:
1.
- Do you use the file author.php for your author's template?
- Do you assign different authors to the posts in the admin panel?
Maor Barazany comments:
2.
You can pass only one post type to the wp_count_post function, so you can use this:
<?php
$review_count = wp_count_posts('review');
$feature_count = wp_count_posts('feature');
$sum = $review_count->publish + $feature_count->publish;
echo '' . $sum;
?>
Maor Barazany comments:
3.
You can use the same function as in #2., just replace the<strong> ->publish </strong> with <strong>->pending</strong>
Maor Barazany comments:
4.
Replace this -
$query= 'author=' . $authorID. '&post_type=feature' . $post_type . '&orderby=post_date&order=asc';
With this :
$query= array('author' => $authorID, 'post_type' => 'feature'. $post_type, 'orderby' => 'post_date', 'order' => 'asc', 'tax_query' => array(
array(
'taxonomy' => 'format',
'field' => 'slug',
'terms' => 'lp'
)
)
);
Maor Barazany comments:
Fix for #2, you should cast the string to integer in order to sum them, so use this -
<?php
$review_count = wp_count_posts('review');
$feature_count = wp_count_posts('feature');
$sum = (int)$review_count->publish + (int)$feature_count->publish;
echo '' . $sum;
?>
Jeremy Phillips comments:
Thanks for you quick answers! Here's how they worked for me:
1. I use the author.php template and yes I do change authors of posts within the admin panel.
2. This ended up counting all 'feature' and 'review' custom post types. It displayed the same count for all authors.
3. Same as above
4. Worked like a charm. Solved.
AdamGold answers:
2. You need to pass the user ID, and get all of his posts:
<?php
wp_get_current_user();
$user_posts = count_user_posts( $current_user->ID );
echo $user_posts;
?>
AdamGold comments:
2. Should be something like
<?php global $wpdb;
wp_get_current_user();
$query = "SELECT COUNT(*) FROM wp_posts WHERE post_status = 'pending' AND post_author = '" . $current_user->ID . "'";
$post_count = $wpdb->get_var($wpdb->prepare($query));
$welcomemessage .= "You currently have $post_count submission(s) pending.";
echo $welcomemessage ?>
AdamGold comments:
1. Try
<?php
wp_get_current_user();
$register_date = date("m/Y", strtotime(get_the_author_meta('user_registered', $current_user->ID)));
?>
<p>Member since: </p><?php echo $register_date; ?>
<br />
Jeremy Phillips comments:
Thanks, Adam.
1. When a user is logged in it is displaying the correct register date, although it shows up on every author profile. How would you make it not specific to logged in user?
2. This displays a count of 2 on every author's profile, regardless of who is logged in. Not sure where it got the 2 from.
3. <strong>SOLVED</strong>. That worked well. This functions the same as #1, but that is fine since I have it so it only displays on the logged in user's profile.
4. <strong>SOLVED</strong>
AdamGold comments:
1. Wait, you want it to display the logged in user stats or the current author that you're watching? If the current author, use:
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
<?php
$register_date = date("m/Y", strtotime(get_the_author_meta('user_registered', $curauth->ID)));
?>
<p>Member since: </p><?php echo $register_date; ?>
<br />
Same goes with the other questions. If you want the logged in author stats, use $current_user->ID:
wp_get_current_user();
// Use: $current_user->ID
If you want the current author you are watching, use $curauth like:
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
// Use: $curauth->ID
Adam
AdamGold comments:
So for question 2..
<?php
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
$user_posts = count_user_posts( $curauth->ID );
echo $user_posts;
?>
Jeremy Phillips comments:
Great, the registration date, #1 is solved.
I'm just having some trouble implementing your advice into #2, the post count issue. The code you originally gave me didn't seem to be working for even the logged-in user.
Jeremy Phillips comments:
Hang on, missed your last post. Let me see if I can get that to work...
Jeremy Phillips comments:
Hang on, missed your last post. Let me see if I can get that to work...
Jeremy Phillips comments:
Ok, so I'm not getting that code to work. The author page queries all posts by the author using $authorID = $curauth->ID and when I inserted your code it made all posts show up on all author profiles. It also displays the same count for everyone and the count is not the actual number of total posts.
AdamGold comments:
Can I please the see whole code which displays your author page & posts count?
Jeremy Phillips comments:
I put it up [[LINK href="http://pastie.org/1944442"]]here[[/LINK]]
Thanks for looking at it.
AdamGold comments:
Okay, try
http://pastie.org/1944532
Jeremy Phillips comments:
Hmm, I'm getting different counts for each author, although they're not correct. Its giving admin a count of 2 and everyone else 1.
AdamGold comments:
Then try
http://pastie.org/1956309
Jeremy Phillips comments:
Thanks, that is better. I'm getting different counts for each user that are close and that respond when I add/remove a post. Although, the numbers aren't quite right. For example admin has a count of around 400 where on the whole site there's only about 20 posts. Admin's count responds funny to pages. When I delete a page the number goes up. When I empty the trash it goes down by a few. Do pages need to be excluded from the count?
AdamGold comments:
Well, we need to try anything ;)
http://pastie.org/1956856
Jeremy Phillips comments:
Ok! I figured out what needs to happen. Your last code does successfully count regular posts that are published and pending. The problem is that I use the custom post types of 'review' and 'feature'. Also it needs to only count published posts.
When I edited you code below to count the post_type of review it worked as it should:
<?php
$query = "SELECT COUNT(*) FROM wp_posts WHERE post_author = '" . $curauth->ID . "' AND post_type = 'review'" ;
$user_posts = $wpdb->get_var($wpdb->prepare($query));
echo $user_posts;
?>
So would you have to use a technique like Maor Barazany's from above to count each post type and echo a summed value? That and to specify it to only include published posts?
<?php
$review_count = wp_count_posts('review')
$feature_count = wp_count_posts('feature');
$sum = $review_count->publish + $feature_count->publish;
echo '' . $sum;
?>
I don't really know what I'm doing here, but something like this?
$review_count = "SELECT COUNT(*) FROM wp_posts WHERE post_author = '" . $curauth->ID . "' AND post_type = 'review'" ;
$feature_count = "SELECT COUNT(*) FROM wp_posts WHERE post_author = '" . $curauth->ID . "' AND post_type = 'feature'" ;
$sum = $review_count->publish + $feature_count->publish;
echo '' . $sum;
AdamGold comments:
Sorry, didn't notice you want this to count custom post types. Your code almost works:
$review_count = "SELECT COUNT(*) FROM wp_posts WHERE post_author = '" . $curauth->ID . "' AND post_type = 'review'" ;
$feature_count = "SELECT COUNT(*) FROM wp_posts WHERE post_author = '" . $curauth->ID . "' AND post_type = 'feature'" ;
$sum = $review_count + $feature_count;
echo '' . $sum;
Jeremy Phillips comments:
Just like the code I messed with above, yours still echos 0. When I use Maor Barazany's code as is, it does successfully count all reviews and features from all authors. Also, shouldn't we leave in the "->publish" part in order to exclude pending posts?
The code does seem like it should work. Not sure why it doesn't. Any other ideas?
AdamGold comments:
Stupid mistake.
$review_count_query = "SELECT COUNT(*) FROM wp_posts WHERE post_author = '" . $curauth->ID . "' AND post_type = 'review'" ;
$feature_count_query = "SELECT COUNT(*) FROM wp_posts WHERE post_author = '" . $curauth->ID . "' AND post_type = 'feature'" ;
$review_count = $wpdb->get_var($wpdb->prepare($review_count_query));
$feature_count = $wpdb->get_var($wpdb->prepare($feature_count_query));
$sum = $review_count + $feature_count;
echo '' . $sum;
Jeremy Phillips comments:
That was great, thanks! Just one more thing, how would you limit it to only published posts? Right now it counts drafts and trash.
When I try to add this, like in Maor Barazany's code, I returns 0:
$sum = $review_count->publish + $feature_count->publish + $mix_count->publish;
AdamGold comments:
Maor's code is using a function to count all posts, my code is a SQL query so there is no method such as publish. You need to add a parameter to the query:
$review_count_query = "SELECT COUNT(*) FROM wp_posts WHERE post_author = '" . $curauth->ID . "' AND post_type = 'review' AND post_status = 'published'" ;
$mix_count_query = "SELECT COUNT(*) FROM wp_posts WHERE post_author = '" . $curauth->ID . "' AND post_type = 'post' AND post_status = 'published'" ;
$feature_count_query = "SELECT COUNT(*) FROM wp_posts WHERE post_author = '" . $curauth->ID . "' AND post_type = 'feature' AND post_status = 'published'" ;
$review_count = $wpdb->get_var($wpdb->prepare($review_count_query));
$feature_count = $wpdb->get_var($wpdb->prepare($feature_count_query));
$mix_count = $wpdb->get_var($wpdb->prepare($mix_count_query));
$sum = $review_count + $feature_count + $mix_count;
echo '' . $sum;
The mix_count is the number of normal posts.