Webmaster Forum


Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


Reply
 
LinkBack Thread Tools Display Modes
Share |
  #1 (permalink)  
Old 04-17-2012, 10:50 AM
fize4ever's Avatar
Contributing Member
Latest Blog:
None

 
Join Date: 02-06-11
Location: /dev/null
Posts: 116
iTrader: 0 / 0%
Count User Posts By Type (WP-Related)

I'm trying to get this function to work, but it doesn't output anything.

Shouldn't this work?

PHP Code:
function count_user_posts_by_type($userid$post_type='member') {

global 
$wpdb;

$where get_posts_by_author_sql($post_typeTRUE$userid);

$count $wpdb->get_var"SELECT COUNT(*) FROM $wpdb->posts $where);

return 
apply_filters('get_usernumposts'$count$userid);

}

count_user_posts_by_type(19); 
__________________
if($these_sigs_need_to_stop == 'Definitely...')
{
echo "Definitely...";
}
else
{
echo "Nuh-uh!";
}
 
Reply With Quote
  #2 (permalink)  
Old 04-17-2012, 01:15 PM
nemrod's Avatar
v7n Mentor
 
Join Date: 12-21-10
Location: Sweden
Posts: 461
iTrader: 0 / 0%
There are two problems.

First is that you've changed the default value for the $post_type argument from 'post' to 'member'. 'member' isn't a post type and so 0 will always be returned (unless you pass an argument that replaces it). Change it back to post or page or any other valid post type.

Second is that you're not actually displaying the result. The function returns an integer, but you never echo it. "echo count_user_posts_by_type(19);" will do the trick.
 
Reply With Quote
  #3 (permalink)  
Old 04-18-2012, 04:30 AM
fize4ever's Avatar
Contributing Member
Latest Blog:
None

 
Join Date: 02-06-11
Location: /dev/null
Posts: 116
iTrader: 0 / 0%
Quote:
Originally Posted by nemrod View Post
There are two problems.

First is that you've changed the default value for the $post_type argument from 'post' to 'member'. 'member' isn't a post type and so 0 will always be returned (unless you pass an argument that replaces it). Change it back to post or page or any other valid post type.

Second is that you're not actually displaying the result. The function returns an integer, but you never echo it. "echo count_user_posts_by_type(19);" will do the trick.
That worked, so thank you Mr. Nice Viking!

I've registered member as a custom post type, and the user of the ID value 19 has created some posts on it, and I want to count these posts.

You're right, it works for post and page, so how should I make it work with the custom post type of 'member'?
__________________
if($these_sigs_need_to_stop == 'Definitely...')
{
echo "Definitely...";
}
else
{
echo "Nuh-uh!";
}
 
Reply With Quote
  #4 (permalink)  
Old 04-18-2012, 05:01 AM
nemrod's Avatar
v7n Mentor
 
Join Date: 12-21-10
Location: Sweden
Posts: 461
iTrader: 0 / 0%
I would construct the WHERE clause manually instead of using get_posts_by_author_sql() since it doesn't seem to work with 'member' for me (though that could be because I don't have the post type).
Set $where to "WHERE post_author = $userid AND post_type = '$post_type' AND (post_status = 'publish')" and see if that works.
 
Reply With Quote
  #5 (permalink)  
Old 04-18-2012, 06:29 AM
fize4ever's Avatar
Contributing Member
Latest Blog:
None

 
Join Date: 02-06-11
Location: /dev/null
Posts: 116
iTrader: 0 / 0%
Doesn't seem to quite work.

Here's how it looks with me:
PHP Code:
function count_user_posts_by_type($userid$post_type='member') { 

global 
$wpdb

$where WHERE post_author $userid AND post_type '$post_type' AND (post_status 'publish');

$count $wpdb->get_var"SELECT COUNT(*) FROM $wpdb->posts $where); 

return 
apply_filters('get_usernumposts'$count$userid); 


__________________
if($these_sigs_need_to_stop == 'Definitely...')
{
echo "Definitely...";
}
else
{
echo "Nuh-uh!";
}
 
Reply With Quote
  #6 (permalink)  
Old 04-18-2012, 06:37 AM
nemrod's Avatar
v7n Mentor
 
Join Date: 12-21-10
Location: Sweden
Posts: 461
iTrader: 0 / 0%
Quote:
Originally Posted by fize4ever View Post
Doesn't seem to quite work.

Here's how it looks with me:
PHP Code:
function count_user_posts_by_type($userid$post_type='member') { 

global 
$wpdb

$where WHERE post_author $userid AND post_type '$post_type' AND (post_status 'publish');

$count $wpdb->get_var"SELECT COUNT(*) FROM $wpdb->posts $where); 

return 
apply_filters('get_usernumposts'$count$userid); 


I'd write it like this:
PHP Code:
function count_user_posts_by_type($userid$post_type 'member') {
    global 
$wpdb;
    
$query "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = $userid AND post_type = '$post_type'";
    
$count $wpdb->get_var($query);
    return 
apply_filters('get_usernumposts'$count$userid);
}
echo 
count_user_posts_by_type(19); 
I merged the strings and removed the publish status since I don't know what status posts of the member type will have. You can add it back if needed. I'm not sure what the filter is supposed to do, you could probably simplify it further by removing that and simply returning $count.
 
Reply With Quote
  #7 (permalink)  
Old 04-18-2012, 07:06 AM
fize4ever's Avatar
Contributing Member
Latest Blog:
None

 
Join Date: 02-06-11
Location: /dev/null
Posts: 116
iTrader: 0 / 0%
Marry me.
__________________
if($these_sigs_need_to_stop == 'Definitely...')
{
echo "Definitely...";
}
else
{
echo "Nuh-uh!";
}
 
Reply With Quote
  #8 (permalink)  
Old 04-18-2012, 07:21 AM
nemrod's Avatar
v7n Mentor
 
Join Date: 12-21-10
Location: Sweden
Posts: 461
iTrader: 0 / 0%
Quote:
Originally Posted by fize4ever View Post
Marry me.
Haha, I'll take it that it worked then, good.
 
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



V7N Network
Get exposure! V7N I Love Photography V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 08:45 PM.
Powered by vBulletin
Copyright © 2000-2013 Jelsoft Enterprises Limited.
Copyright © 2003 - 2013 Escalate Media LP




Search Engine Optimization by vBSEO 3.6.0 RC 2 ©2011, Crawlability, Inc.