Opened 10 years ago
Closed 10 years ago
#32243 closed enhancement (fixed)
count_user_posts Should Accept Multiple Post Types
Reported by: | nikonratm | Owned by: | boonebgorges |
---|---|---|---|
Milestone: | 4.3 | Priority: | normal |
Severity: | normal | Version: | 4.3 |
Component: | Users | Keywords: | has-patch needs-testing needs-docs |
Focuses: | template | Cc: |
Description
It's great that the count_user_posts function now accepts a post_type parameter as of 4.1 but I think it would be much more useful if it accepted multiple post types, I think I will go ahead and implement this =)
Attachments (8)
Change History (19)
This ticket was mentioned in Slack in #core by mooner. View the logs.
10 years ago
#3
@
10 years ago
- Keywords needs-patch added; has-patch needs-testing removed
Thanks for the patch, nikonratm!
Your $cap_final
logic doesn't look like it will work properly. Say I'm querying for three post types: array( 'foo', 'bar', 'baz' )
. And let's say that I have the read_private_posts
cap for 'baz' but not for 'foo' and 'bar'. In this situation, it looks like the last post type cap will overwrite the previous two, and private posts for all three post types will be returned. If we're going to add the improvement suggested in this ticket, the SQL will need to look like this:
... WHERE ( ( post_type = 'foo' AND post_status = 'publish' ) OR ( post_type = 'bar' AND post_status = 'publish' ) OR ( post_type = 'baz' AND ( post_status = 'publish' OR post_status = 'private' ) ) )
(You'll have to make sure the "users can view their own private posts" stuff works too.)
It'll help enormously to write some unit tests describing scenarios like the above. See https://core.trac.wordpress.org/browser/trunk/tests/phpunit/tests/post/getPostsByAuthorSql.php for some examples. (You could also write indirect tests for count_user_posts()
.)
Small additional point: Make sure you don't break backward compatibility for singular $post_type
. If a string has been passed, cast it to array( $post_type )
.
#4
@
10 years ago
- Keywords has-patch needs-testing needs-unit-tests added; needs-patch removed
Ah, good point. I've updated the function to do as you suggested, as well as allow for string (i.e. single)
$post_types
arguments.
I don't have the time to write tests right now but I would be happy to do that.
#5
@
10 years ago
- Keywords needs-unit-tests removed
Cool, added a unit test for multiple post types and for testing when a user has access to private posts for only certain post types ( test_user_has_access_only_to_private_posts_for_certain_post_types
)
#7
@
10 years ago
- Owner set to boonebgorges
- Resolution set to fixed
- Status changed from new to closed
In 32523:
#8
@
10 years ago
- Keywords needs-docs added
- Resolution fixed deleted
- Status changed from closed to reopened
Since we added an additional type for $post_type
, we need to note that in the changelogs as well.
#9
follow-up:
↓ 10
@
10 years ago
DrewAPicture - Can you be more specific? I added a changelog item to get_posts_by_author_sql()
. I didn't add the same thing to all functions that call get_posts_by_author_sql()
, though I did update the param definitions. Please give guidance on how you want to handle this.
#10
in reply to:
↑ 9
@
10 years ago
Replying to boonebgorges:
DrewAPicture - Can you be more specific? I added a changelog item to
get_posts_by_author_sql()
. I didn't add the same thing to all functions that callget_posts_by_author_sql()
, though I did update the param definitions. Please give guidance on how you want to handle this.
$post_type
only accepted a string before, and now accepts either a string or an array in three places. We need to document that it now accepts an array in the changelog.
Updated count_user_posts to accept multiple post types