Counts of post types for author
-
I am attempting to count the number of entries a particular user has for every post type, including custom post types, so I am aware of content before deleting a user. I am having two problems.
[loop author=this orderby=type status=any] [field post-type] : [field title-link] [/loop] [loop-count]
If I use the loop construct with no “type=” and status= any, I was hoping I would see every post type. However, I only see 4 different post types, one of which is a custom post type. I have many more which are not included, but they are shown if I explicitly put them in “type=”, so it knows they are there. For example, we use bbPress so I can see “topic” and “reply” if I put them with type=, but they do not show up if I remove type=.
The second problem is that I would just like a count for every post type I have. When I remove the [field] part from the loop, it returns a blank line for every entry and then gives me the loop count. I have also tried the [calc] method inside the loop and it also gives a blank line for every entry.
Is there a way for me to show every post type I have and then show the count of each for a particular user?
Thanks, Mike
-
Hello,
In the latest plugin update, I added a way to list post types. For details, please refer to the documentation under Main -> Post Type.
You can do something like:
[for type=all exclude=post,page] Post type: [each name] Post count: [loop-count author=this] [/for]
It should get all post types, including ones specified to be excluded from search, etc.
Thanks for the plugin update, but it is not quite working. The results I see in the database do not match the results from your sample code.
From the database table wp_posts and field post_type …
attachment 371
xxxxconference 12
xxxxpresentation 261
nav_menu_item 52
page 64
post 4
reply 40
revision 5
topic 23
wpcf7_contact_form 4From your results …
Post type: Advanced Custom Fields
Post count: 0Post type: Forum
Post count: 0Post type: Conference
Post count: 0Post type: Presentation
Post count: 0Post type: Page
Post count: 63Post type: Post
Post count: 4Post type: Reply
Post count: 40Post type: Topic
Post count: 23Post type: Logs
Post count: 0Post type: Contact Form
Post count: 0Could it be that the database shows the total number of posts, while the loop is filtering it by current user as author? What if you removed
author=this
? For example, there are 64 total Pages and 63 by current user – which looks like 1 was created by a different author/user.Another possibility is that 1 page is in draft or future status, so it’s not showing up in the loop result. In that case, you might add
status=any
to the loop.As for Conference and Presentation showing as 0 posts, I’m not sure why the loop is not finding them. They could have different authors than the current user.. Are you able to display them using the usual way? Like:
[loop type=presentation author=this]...[/loop]
I am sorry that I forgot to mention that the database numbers are just for me as the author, so it is not an author issue (probably).
I tried the loop with type=conference and it returned 12 items, which matches the database. I also added status=any in the [for] and it had no impact on any results.
I just pushed an update that fixed an issue with [for type] – but it was for when the post types were specified (not “all”) so probably not related to your case. It’s a brand new feature, so there’s room for improvement.
Can you check the
conference
post type by itself?[for type=conference] Post type: [each name] Total: [loop-count] By current user: [loop-count author=this] [/for]
The expected result is 12, and your last test showed 0. I’m trying to narrow down why.
Oh, I just noticed, the first [loop-count] will not work, because without any query parameters, it displays the post count of a loop previously completed (which in this case is nothing).. I made another update now (3.5.0) to fix this.
I have updated to 3.5.1 and your latest code returns 0 for Total and for current user. But does show the post name.
Hmm, I see that it’s counting the post types Topic and Reply correctly, but not Conference or Presentation. I wonder what is different about the latter?
It’s strange that a normal loop can get the posts but not [loop-count]. The shortcode just calls [loop] with the current post type in the [for] loop, then returns the count.
I made an update now, with a debug parameter to diagnose the queries.
Can you try the following?
A: [loop type=conference debug=true][/loop][loop-count] <hr> B: [loop-count type=conference debug=true] <hr> C: [for type=conference][loop-count debug=true][/for]
All three should perform the same query and return the same count. But apparently there is something different about A and the rest.
Here are the results. I cannot see where the 459 is coming from, since there are over 2000 posts with over 700 authored by me. The 17 in B is the number of conferences, with 12 of them authored by me. Also note that I tried your ABC code before I upgraded the plugin and A returned a value of 17 also.
A: 459
<hr>
B: 17
<hr>
C: 0I only see one debug output at the top of the page.
Array ( [post_type] => Array ( [0] => conference ) [ignore_sticky_posts] => 1 [post_status] => Array ( [0] => publish ) [posts_per_page] => -1 )
I added “author=this” to the loop in B. It returned a zero for the count and also produced another debug output.
Array ( [post_type] => Array ( [0] => conference ) [ignore_sticky_posts] => 1 [author__in] => Array ( [0] => 9999 ) [post_status] => Array ( [0] => publish ) [posts_per_page] => -1 )
Thank you for posting the debug output. I see these are very inconsistent results.
I’ve tested all 3 variations, and they’re all displaying the correct results on my end – which leads me to think that something is different about this conference post type. Is it created by a plugin or theme? Could you try the ABC tests with the page post type to see if the results are correct?
I think the best way to solve this is to work with each test until they’re all correct. I appreciate your patience, if you can work through them with me..
A: [loop type=conference debug=true][/loop][loop-count]
The loop is not getting all conference posts (total: 2000~ and test: 459). The debug output shows a simple query – the only thing I can think of changing is the post_status. Are some of the conference posts in draft or future status? If so, could you try:
[loop type=conference status=any debug=true]
B: [loop-count type=conference debug=true]
This one showed no query at all, until you put author=this. That’s so strange.. As described before, this shortcode just passes the parameters to [loop] to count the result. You said that 17 is the total number of conference posts – what about the 2000~ posts you mentioned for A?
C: [for type=conference][loop-count debug=true][/for]
This returns 0 for post count, and does not show a query either. The [loop-count] should be passing the current post type as a query to [loop] to count the result. Can you see if the following displays anything?
[for type=conference] Count post type: [each name] ([loop-count debug=true]) [/for]
If there’s no output at all, then it means the [for] shortcode is not getting the conference post type in the first place.
It could be due to some setting in the conference post type, for example, user login status or capabilities.
In the newest update, I added a debug parameter to the [for] shortcode, to inspect what post types it’s getting.
Could you update the plugin and post the output of this? It could be pretty long – or maybe it will be nothing..
[for type=conference debug=true][/for]
Here is the debug output from your last [for] suggestion. I compared it to the same code type=post and the only important difference I see is that my custom type has “[exclude_from_search] => 1”, but I may not be looking for the same things you are. I am also curious where all these post type attributes are stored. I am an SQL guy, so if these are in the database somewhere I think I could compare all the post types and find the differences.
Also note that you will see in the debug the actual post type I have been using is “hedwconference” with a label of “conference”. All the testing I have done with your code has been using the proper hedwconference post type. It just was not important to correct earlier in this thread.
Array ( [hedwconference] => stdClass Object ( [labels] => stdClass Object ( [name] => Conferences [singular_name] => Conference [add_new] => Add Conference [add_new_item] => Add New Conference [edit_item] => Edit Conference [new_item] => New Conference [view_item] => View Conference [search_items] => Search Conferences [not_found] => No Conferences Found [not_found_in_trash] => No Conferences Found in Trash [parent_item_colon] => [all_items] => Conferences [archives] => Conferences [insert_into_item] => Insert into post [uploaded_to_this_item] => Uploaded to this post [featured_image] => Featured Image [set_featured_image] => Set featured image [remove_featured_image] => Remove featured image [use_featured_image] => Use as featured image [filter_items_list] => Filter posts list [items_list_navigation] => Posts list navigation [items_list] => Posts list [menu_name] => Conferences [edit] => Edit [view] => View Conference [parent] => Parent Conference [name_admin_bar] => Conference ) [description] => Conferences [public] => 1 [hierarchical] => [exclude_from_search] => 1 [publicly_queryable] => 1 [show_ui] => 1 [show_in_menu] => 1 [show_in_nav_menus] => 1 [show_in_admin_bar] => 1 [menu_position] => [menu_icon] => [capability_type] => page [map_meta_cap] => 1 [register_meta_box_cb] => [taxonomies] => Array ( ) [has_archive] => 1 [rewrite] => Array ( [slug] => hedwconference [with_front] => 1 [pages] => 1 [feeds] => 1 [ep_mask] => 1 ) [query_var] => hedwconference [can_export] => 1 [delete_with_user] => [_builtin] => [_edit_link] => post.php?post=%d [label] => Conferences [name] => hedwconference [cap] => stdClass Object ( [edit_post] => edit_page [read_post] => read_page [delete_post] => delete_page [edit_posts] => edit_pages [edit_others_posts] => edit_others_pages [publish_posts] => publish_pages [read_private_posts] => read_private_pages [read] => read [delete_posts] => delete_pages [delete_private_posts] => delete_private_pages [delete_published_posts] => delete_published_pages [delete_others_posts] => delete_others_pages [edit_private_posts] => edit_private_pages [edit_published_posts] => edit_published_pages [create_posts] => edit_pages ) ) )
Here are more results from earlier requests. I changed the ABC tests to look at type=page.
A: [loop type=page status=any debug=true][/loop][loop-count] B: [loop-count type=page debug=true] C: [for type=page] Count post type: [each name] ([loop-count debug=true]) [/for]
This resulted in the following. Note that I have authored 65 pages.
A: 106 B: 102 C: Count post type: Page (102)
In the previous post I mentioned the number 2,000. That is the total number of all post types in my database. That number is misleading since our ABC test was only looking at hedwconferencem which there would be only 17 (12 authored by me). I was just trying to figure out where such a high number (459) would come from.
Progress. I have got the answer of 12 for the hedwconference post type, but only for A and B. When the B logic is put inside the [for] it does not work. If I use author=this I get the right answer of 12 and if I remove it, then I get 17 for AB, but still zero for C.
A: 12
B: 12
C: Count post type: Conference (0 )A: [loop type=hedwconference status=any debug=true author=this][/loop][loop-count] B: [loop-count type=hedwconference debug=true author=this] C: [for type=hedwconference] Count post type: [each name] ([loop-count type=hedwconference debug=true author=this] ) [/for]
Great, that’s helpful information.
Looking at the post type object, I don’t see anything that could be so specific about this post type – as you mentioned “exclude_from_search” stands out, but there’s also “publicly_queryable” set to true.
One thing I can confirm is that [for type] is getting the post type correctly, it’s just not passed to [loop-count] in the case of test C. Strange that it works fine with page but not this custom post type..
There are a few steps involved, passing the post type from [for] to [loop-count] then to [loop]. I’ll try to look into each step and see where it could be going wrong.
I found the bug. [for type] was not passing the post type slug, but instead its singular label. I’m surprised WP_Query was still returning results, that it worked at all. I guess since Page, Post, and other post types I was testing all had the same label as the slug, just capitalized. In the case of your conference post type, the label and the slug differed significantly, which revealed this issue.
Anyway, please try the latest version 3.5.4.
- The topic ‘Counts of post types for author’ is closed to new replies.