Ordering collection of posts

This topic is: not resolved
Viewing 7 posts - 1 through 7 (of 7 total)
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:
foreach ($user->posts(“post_type=timecard”) as $timecard) { … }

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:
foreach ($wf->role(“volunteer”)->users() as $volunteer) { … }

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”)

  • This reply was modified 10 years, 9 months ago by traversal.
February 19, 2014 at 5:18 am #3428
dovetail
Post count: 18

Thanks for your reply.

Regarding Sorting:
I was able to get sorting working with the “by” method – what if I want to order by last name, then first name? Will this accept multiple parameters? I’ve tried a few things but can’t get it to work.

Regarding filtering user posts by type:
This is a pretty central part of my project – do you know approximately when the next release will happen?

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:
foreach ($user->posts()->by(“reservation_fields.reservation_date”) as $reservation) { … }

However, is is possible to use that with a “DESC” order? This didn’t work:
foreach ($user->posts()->by(“reservation_fields.reservation_date DESC”) as $reservation) { … }

** 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.

  • This reply was modified 10 years, 9 months ago by dovetail.
February 19, 2014 at 7:34 am #3431
dovetail
Post count: 18

Nevermind the last post – I found the documentation under “WOOF-Collection:”
https://www.masterpressplugin.com/docs/developer/classes/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.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

Latest From the Blog

Ability to deactivate licence domains within MasterPress account management

25th October 2024

We have just rolled out an account management feature which allows licence holders to have better control over the domains that use their licence key. From the MasterPress Account Management page, the list of Active Sites that use your licence now includes a function that allows you to remove a domain from your licence. Simply… 

Plugin Requirements

MasterPress requires a minimum of WordPress version 4.9, MySQL 5.6, and PHP version 5.6.20.

We also recommend that PHP is configured to use a memory limit of 64MB per request (128MB may be required for sites with higher complexity).

This plug-in is not compatible with the WordPress.com hosted service.

Three AM