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
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
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
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:


  • This reply was modified 10 years, 5 months ago by traversal.
February 19, 2014 at 5:18 am #3428
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
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, 5 months ago by dovetail.
February 19, 2014 at 7:34 am #3431
Post count: 18

Nevermind the last post – I found the documentation under “WOOF-Collection:”

February 19, 2014 at 2:11 pm #3432
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:


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

MasterPress 1.4.1 is now available

27th May 2024

This update addresses backwards compatibility issues that were introduced in the previous update. There were some edge cases where PHP 7 had issues and it didn’t work at all on PHP 5.6. We have now changed it so that the PHP 8 compatibility changes will now only affect PHP 8 and will be more thorough… 

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