Support

Home Forums Event Espresso Premium How to sort People Archive by Last Name?

How to sort People Archive by Last Name?

Posted: May 6, 2015 at 7:00 am

Viewing 6 reply threads


Brandon Gannon

May 6, 2015 at 7:00 am

I’m using the People Addon to list instructors. The problem is that by default the archive lists them chronologically, with the most recently entered at the top. What I want is to sort by last name. Should be possible, as there is a last name field.
So I’ve tried orderby=title which sorts by first name.
And then I tried orderby=meta_value&meta_key=….
and this is where I fail. I’ve tried:
lname, PER_lname, ATT_lname.
But those all break the archive and return no results.
What am I missing? THANKS!

Code (I only use one line at a time and comment out what I’m not using):

query_posts($query_string . '&orderby=title&order=ASC'); // THIS ONE WORKS, A LITTLE BIT
//query_posts($query_string . '&orderby=lname&order=ASC');
//query_posts($query_string . '&meta_key=PER_lname&orderby=meta_value&order=ASC');
//query_posts($query_string . '&post_type=espresso_people&orderby=meta_value&meta_key=ATT_lname');


Josh

  • Support Staff

May 6, 2015 at 4:17 pm

Hi Brandon,

If you take a look in the database you’ll find that the last name of a “People” custom post type is stored in the _esp_attendee_meta, in the ATT_lname column. So you could do a join on _esp_attendee_meta table and that will make it possible to order by that value. _esp_attendee_meta.ATT will equal the _posts.ID.

-or-

If you input the title of the post in this format: “Last Name, First Name”, the CPT slug will be /people/lastname-firstname. So with that, orderby=name will just work without doing any joins on custom tables. Note the title field is above the First Name and Last Name fields in the post editor.


Brandon Gannon

May 7, 2015 at 7:08 am

OK so something like this? In functions.php ?

global $wpdb;

$query = "SELECT prefix_esp_attendee_meta.ATT_ID, prefix_esp_attendee_meta.ATT_lname
  FROM prefix_esp_attendee_meta
  JOIN prefix_posts ON prefix_posts.ID = prefix_esp_attendee_meta.ATT_ID";

$results = $wpdb->get_results($query);

And then this:
query_posts($query_string . ‘&orderby=meta_value&meta_key=ATT_lname’);

Before the WHILE in the archive template?
I’ve tried this and it gives me no search results.
What am I missing?

THANKS!


Josh

  • Support Staff

May 7, 2015 at 1:47 pm

If you add your function in functions.php, you’ll need to make sure you do a check for the espresso_people post type so you don’t change the query for other post types.

Along with that, you do not need to rewrite the entire query because you can use the posts_orderby and the posts_join_paged filters. This way you can conditionally add the orderby statement to the query for the espresso_people post type archive. There’s some example code for these filters in the WordPress Codex:

https://codex.wordpress.org/Plugin_API/Filter_Reference/posts_orderby

Beyond that, I can recommend trying gets_posts() or WP_Query(). I don’t have much experience with using query_posts() because it’s generally not recommended.


Josh

  • Support Staff

May 7, 2015 at 2:55 pm

There’s some more good information about how to make custom query modifications in these two codex articles too:

https://codex.wordpress.org/Custom_Queries
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts


Brandon Gannon

May 7, 2015 at 4:13 pm

Understood. Thanks.
I understand the limits of support.
But wouldn’t you expect that a list of people should at least have the option of sorting by last name? I’d even expect it to be the default.
I can’t be the only one who wants this, so I’d suggest adding the feature.
Otherwise, ticket closed. Thanks for your help!


Dean

May 11, 2015 at 3:18 am

Hi Brandon,

I think we’ve had one or two requests for this over the years, but there hasn’t been a huge demand for it. I’ll certainly add it as a feature request for you.

I’ll also mark this as resolved. If you need to continue this discussion please open up a new forum thread and link to this one. Thanks!

Viewing 6 reply threads

The support post ‘How to sort People Archive by Last Name?’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso