Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Author post count include custom posts? WordPress

I was using the below to show user/author post count.

Now that I have moved all my user submitted items to a custom post type this is showing 0.

Help!


Added: <a href="/author/<?php global $authordata; echo $authordata->user_login; ?>"><?php the_author_posts(); ?></a>

Answers (4)

2011-08-01

Kristin Falkner answers:

Have you declared author in the supports array when registering the custom post type? Custom post types just default to title and editor. Ex:

'supports' => array('title','editor','author','excerpt','comments')

More info:
http://codex.wordpress.org/Function_Reference/register_post_type


kateM82 comments:

Yes i have. thanks.

2011-08-01

Romel Apuya answers:

how about trying this one.

<?php
global $authordata;
$post_author = $authordata->ID; //author id
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = $post_author AND post_type IN ('post','page','book') and post_status = 'publish'" );
?>
<a href="/author/<?php echo $authordata->user_login; ?>"><?php echo $count; ?></a>


kateM82 comments:

Where do I put this bit?


<?php

global $authordata;

$post_author = $authordata->ID; //author id

$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = $post_author AND post_type IN ('post','page','book') and post_status = 'publish'" );

?>




I am using this code in a PHP widget, and it just crashed the site (white screen).


Here is the full widget code:

<div style="min-height: 180px; padding-bottom:10px;"><h1 style="margin-top:-10px; padding-bottom:10px;"><?php global $authordata; echo $authordata->display_name; ?></h1>
<div class ="user_info_avatar" style="float:left; padding:5px; height:96px; margin:5px;"><?php global $authordata; echo get_avatar($authordata->ID, '96'); ?> </div>
<ul style="list-style:none;">
<li style="margin-bottom: 3px; padding-top:10px; font-size:19px;">Added: <a href="/author/<?php global $authordata; echo $authordata->user_login; ?>"><?php the_author_posts(); ?></a>
</li>
<li style="margin-bottom: 3px; font-size:19px;"> <?php global $authordata; echo cp_displayPoints($authordata->ID); ?></li>

<li style="margin-bottom: 3px; padding-top:10px; font-size:13px;"> Role: <?php global $authordata; echo cp_module_ranks_getRank($authordata->ID);?></li>
<li style="margin-bottom: 3px; padding-top:10px; font-size:13px;"><a href="/author/<?php global $authordata; echo $authordata->user_login; ?>">MY FASHION CUPBOARD &gt;&gt;</a> </li>
<li style="margin-bottom: 3px;"><a href="<?php global $authordata; echo $authordata->user_url; ?>"> <?php global $authordata; echo $authordata->user_url; ?></a></li>
</ul>
</div>


Romel Apuya comments:

try replacing your widget code with this one..
why are you declaring the global $authordata many times? when you can just declare it once?
this code should be inside your widget.
but before you do that try backing up your old code.
thanks.

<?php
global $authordata;
$post_author = $authordata->ID; //author id
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = $post_author AND post_type IN ('post','page','book') and post_status = 'publish'" );
?>
<a href="/author/<?php echo $authordata->user_login; ?>"><?php echo $count; ?></a>

<div style="min-height: 180px; padding-bottom:10px;">
<h1 style="margin-top:-10px; padding-bottom:10px;">
<?php echo $authordata->display_name; ?></h1>

<div class ="user_info_avatar" style="float:left; padding:5px; height:96px; margin:5px;">
<?php echo get_avatar($authordata->ID, '96'); ?>
</div>

<ul style="list-style:none;">

<li style="margin-bottom: 3px; padding-top:10px; font-size:19px;">
Added: <a href="/author/<?php echo $authordata->user_login; ?>"><?php echo $count; ?></a>
</li>

<li style="margin-bottom: 3px; font-size:19px;">
<?php echo cp_displayPoints($authordata->ID); ?>
</li>

<li style="margin-bottom: 3px; padding-top:10px; font-size:13px;">
Role: <?php echo cp_module_ranks_getRank($authordata->ID);?>
</li>

<li style="margin-bottom: 3px; padding-top:10px; font-size:13px;">
<a href="/author/<?php echo $authordata->user_login; ?>">MY FASHION CUPBOARD &gt;&gt;</a>
</li>

<li style="margin-bottom: 3px;">
<a href="<?php echo $authordata->user_url; ?>"> <?php echo $authordata->user_url; ?></a>
</li>

</ul>
</div>


kateM82 comments:

White screen again :(


Romel Apuya comments:

how are you calling the widget?


Romel Apuya comments:

is this present in your functions.php?

add_filter('widget_text', 'php_text', 99);

function php_text($text) {
if (strpos($text, '<' . '?') !== false) {
ob_start();
eval('?' . '>' . $text);
$text = ob_get_contents();
ob_end_clean();
}
return $text;
}


kateM82 comments:

No it wasn't.

I added it in and repasted your widget code and still got the white screen.

You can view the old (only counting real posts, not custom posts) widget working here - http://dropdeadgorgeousdaily.com/dshop/rachel-zoe-capejacket/


Romel Apuya comments:

try declaring

$global $wpdb;

below of after


global $authordata;


kateM82 comments:

Sorry Romel,

Can you please paste the complete code, bit of a newb here.

Can you paste what you think should be in the widget, and what needs to go into the custom functions file.

thanks.


Romel Apuya comments:

here: in you widget code..


<?php
global $authordata;
global $wpdb;

$post_author = $authordata->ID; //author id
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = $post_author AND post_type IN ('post','page','book') and post_status = 'publish'" );
?>
<a href="/author/<?php echo $authordata->user_login; ?>"><?php echo $count; ?></a>
<div style="min-height: 180px; padding-bottom:10px;">
<h1 style="margin-top:-10px; padding-bottom:10px;">
<?php echo $authordata->display_name; ?></h1>
<div class ="user_info_avatar" style="float:left; padding:5px; height:96px; margin:5px;">
<?php echo get_avatar($authordata->ID, '96'); ?>
</div>
<ul style="list-style:none;">
<li style="margin-bottom: 3px; padding-top:10px; font-size:19px;">
Added: <a href="/author/<?php echo $authordata->user_login; ?>"><?php echo $count; ?></a>
</li>
<li style="margin-bottom: 3px; font-size:19px;">
<?php echo cp_displayPoints($authordata->ID); ?>
</li>
<li style="margin-bottom: 3px; padding-top:10px; font-size:13px;">
Role: <?php echo cp_module_ranks_getRank($authordata->ID);?>
</li>
<li style="margin-bottom: 3px; padding-top:10px; font-size:13px;">
<a href="/author/<?php echo $authordata->user_login; ?>">MY FASHION CUPBOARD &gt;&gt;</a>
</li>
<li style="margin-bottom: 3px;">
<a href="<?php echo $authordata->user_url; ?>"> <?php echo $authordata->user_url; ?></a>
</li>
</ul>
</div>

2011-08-01

ej_emman answers:

hello kateM82,

try this code.. place it where you want to display this code...Hope this help..


global $wpdb;
$user_id = $post->post_author; //change this if not in a std post loop
$where = "WHERE post_author = {$user_id} and post_status='publish'";
$post_count = $wpdb->get_var(
"SELECT COUNT( * ) AS total
FROM {$wpdb->posts}
{$where}
");
$user = get_userdata($user_id);
$post_count = get_usernumposts($user->ID);
echo $user->display_name.'-> '.$post_count; // the output

P.M me at skype: ej_emman


ej_emman comments:

you can also try this simple code:

very easy code...

list_authors(TRUE, TRUE, TRUE)


kateM82 comments:

Hi, can you please show me how you add this to the widget code.

The code I use (and it's just something I hacked together, I am not a coder, so apologies ) is here:

<div style="min-height: 180px; padding-bottom:10px;"><h1 style="margin-top:-10px; padding-bottom:10px;"><?php global $authordata; echo $authordata->display_name; ?></h1>

<div class ="user_info_avatar" style="float:left; padding:5px; height:96px; margin:5px;"><?php global $authordata; echo get_avatar($authordata->ID, '96'); ?> </div>

<ul style="list-style:none;">

<li style="margin-bottom: 3px; padding-top:10px; font-size:19px;">Added: <a href="/author/<?php global $authordata; echo $authordata->user_login; ?>"><?php the_author_posts(); ?></a>

</li>

<li style="margin-bottom: 3px; font-size:19px;"> <?php global $authordata; echo cp_displayPoints($authordata->ID); ?></li>



<li style="margin-bottom: 3px; padding-top:10px; font-size:13px;"> Role: <?php global $authordata; echo cp_module_ranks_getRank($authordata->ID);?></li>

<li style="margin-bottom: 3px; padding-top:10px; font-size:13px;"><a href="/author/<?php global $authordata; echo $authordata->user_login; ?>">MY FASHION CUPBOARD &gt;&gt;</a> </li>

<li style="margin-bottom: 3px;"><a href="<?php global $authordata; echo $authordata->user_url; ?>"> <?php global $authordata; echo $authordata->user_url; ?></a></li>

</ul>

</div>




This is pasted into a PHP widget. If I need to add anything to the custom functions file etc.. please let me know. thanks.


ej_emman comments:

Try replacing your code:

//From:
Added: <a href="/author/<?php global $authordata; echo $authordata->user_login; ?>"><?php the_author_posts(); ?></a>


//To
Added: <?php list_authors(TRUE, TRUE, TRUE); ?>


kateM82 comments:

That just showed a list of all 770 authors on the site :(