WordPress action to sort a relational field in a custom post type
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Hi. I am using Pods to manage + display custom post types. I have an events CPT, which I have created a Pods template to display on the frontend in a page. In this template I want to have any people who are playing in an event be listed; I've gotten this far by making playing a relational field between my events and people CPTs. However, I am struggling to sort people that are playing by peoples last_name meta value.

I am able to sort the playing field in my Pods settings, which does auto-sort the field in the event post editor, but I need them to be sorted this on the frontend inside my Pods template, which looks like this:

[if playing]
    Playing: [each playing]<a href="{@playing.permalink,esc_url}">{@playing.title}</a>[/each]

I have tried following the docs here and the question here, and have also attempted to use this snippet (below) but I keep coming up short.

function sort_by_lastname_func( $query_args ) {
    $query_args['meta_key'] = 'last_name';
    $query_args['orderby'] = 'meta_value';
    $query_args['order'] = 'ASC';
    return $query_args;

I felt closest with the linked question but couldn't wrap my head around how to include the shortcode inside of the first shortcode in my template.

Thank you, please let me know if I can provide more info.

Hello @sharper. Can you export the Pods CPTs, so that I can test it on my local WP installation? Also, can you share the rest of the custom code in some way so that we can work locally? Is there a test server available to work directly?
kostasx over 1 year ago
Hi Kostasx, yes, here is my export: https://pastebin.com/KJfydHSY The fields are named differently than my post as I was experimenting locally but I expect you'll see the same issue. I don't have a webserver to share as I am working locally. I believe that the export above should provide you with the exact template and everything but please let me know if that isn't what you need, Thank you
sharper over 1 year ago
Hello again. Can you also export the Events/People entries (sample custom posts), probably using the WP XML exporter so that I can import them and work on actual data? Also, by playing do you mean "Is Speaking at"?
kostasx over 1 year ago
Hello, yes, I can get you some example data as soon as I get back in front of my computer. And yes, that is correct.
sharper over 1 year ago
Great. Be sure, also to include the exact steps that you are following, e.g. 'I create a page, insert this kind of Pod, with this kind of template, etc.'. Or you can send the page content instead just to make sure we are on the same track here, as Pods framework is giving me some headaches. ;)
kostasx over 1 year ago
awarded to kostasx

Crowdsource coding tasks.

1 Solution

Winning solution

I managed to display the people ordered by their last name following the steps below:

1) I modified the Event List (removed some lines for clarity)

Pods Admin: Templates > Event List

<div class="event-list">
  <h2><a href="{@permalink}">{@post_title}</a></h2>
      [pods name="person" where="is_speaking_at.ID={@ID}" template="Sorted Speakers" orderby="last_name DESC"]
  [if moderators]
    <br/><i class="fas fa-microphone"></i> Moderator(s): {@moderators}

2) I create a Sorted Speakers Template:

Pods Admin: Templates > Sorted Speakers:


3) I added the Event List Pod shortcode in a page:


[pods name="event" slug="263" template="Event List" shortcodes=1]

The number 263, is the id of the Event. You must choose the one you want displayed.

4) In wp-config.php I added:


You can change the DESC to ASC, for ordering by last name in Ascending order if you like

I have also created a WordPress site online with everything in place. Here is the sorted page:

You can PM me to send you the credentials in order to login and check the code in this demo WP installation.

Hope this helps you.

Hi Kostasx. This looks very close. I am wanting to do what you provided in your example page, but as a list. E.g., I am using my Event Pods Auto Template Options setting, and I am wanting to list the list of Ordered Speakers on a single line inside of the Events List template. Does that make more sense? Thanks again.
sharper over 1 year ago
Can you send me the content / template of the page you want to create? Or maybe add it to the WP demo site and then I can take a look at it?
kostasx over 1 year ago
Hi Kostasx, I am awarding you the solution as you have gone above and beyond. I was able to solve it from there. Thank you :)
sharper over 1 year ago