- This topic has 6 replies, 2 voices, and was last updated 10 years, 10 months ago by traversal.
Author | Posts |
---|---|
February 15, 2014 at 3:34 am #3422 | |
dovetail
Post count: 18
|
I have two related questions. (1) I am getting a collection of posts authored by a particular user. It is working with this code: But they are being ordered by the post publish date. Timecards have a custom field set called “timecard_fields” and a field in that set called “timecard_date” – I want to order this collection of posts by that date value. Any ideas (2) I am listing all the users of type “volunteer.” It’s working, with this code: But again, I want to order the users by lastname/firstname instead of whatever the default is. Any help is very much appreciated! |
February 15, 2014 at 4:20 am #3423 | |
dovetail
Post count: 18
|
re: #1 – I just discovered that the query is NOT filtering by post_type. It’s returning ALL posts that are authored by the user. So I guess I also need to know how to return a set of posts authored by a user of a particular custom post type. |
February 18, 2014 at 2:12 pm #3426 | |
traversal
Post count: 207
|
Hi there. I’ve just tried to recreate this scenario and can confirm there are some issues with the API when listing user posts by post type. Exactly like you stated, it’s currently returning ANY type of post. Sorry, this will be fixed in the next release. As for your second question, the users method returns a WOOF_Collection object, which has a “by” method to sort by any property / method name. So you can do this: $wf->role(“volunteer”)->users->by(“last_name”)
|
February 19, 2014 at 5:18 am #3428 | |
dovetail
Post count: 18
|
Thanks for your reply. Regarding Sorting: Regarding filtering user posts by type: |
February 19, 2014 at 5:32 am #3429 | |
dovetail
Post count: 18
|
This question goes with the one above – I tried using the “by” method to sort some posts by a custom field, and it worked great using this syntax: However, is is possible to use that with a “DESC” order? This didn’t work: ** Nevermind this one, I guessed and figured out that this is the proper syntax: foreach ($user->posts()->by(“reservation_fields.reservation_date”, “DESC”) as $reservation) { … } Testament to the intuitiveness of this framework! Next question: How to make it order by a field numerically. Perhaps this is documented somewhere? I couldn’t find it in the docs.
|
February 19, 2014 at 7:34 am #3431 | |
dovetail
Post count: 18
|
Nevermind the last post – I found the documentation under “WOOF-Collection:” |
February 19, 2014 at 2:11 pm #3432 | |
traversal
Post count: 207
|
Hey, glad you had some luck working out the descending order. There is a way to order by last name then first name, but it requires a custom extension of the MEOW_User class to create a method that you can then sort on. Check out this example, where you can create a simple class called “MY_User” to extend MEOW_User (the master press user class), which will then be used as the class returned by all methods that return a user in the API. Once you have that, “last_then_first_name” is also a sortable property. Pretty powerful: https://gist.github.com/traversal/17ada9ecb8ee24af7e51 That same technique applies for sorting on custom fields and so on too. All you need to do is create a function to return a sortable string, and the sort methods can sort on that new function’s result. Just need to be careful not to name the functions the same as any built-in functions for the class you’re extending 🙂 As for the next release, I’ve been working away this afternoon to fix the issue with users and posts for certain post types. A lot harder than I thought actually, but fixed now. The next release will be sometime during this week, hopefully tomorrow. |
You must be logged in to reply to this topic.