Posted: 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. Code (I only use one line at a time and comment out what I’m not using):
|
|
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. |
|
OK so something like this? In functions.php ?
And then this: Before the WHILE in the archive template? THANKS! |
|
If you add your function in functions.php, you’ll need to make sure you do a check for the 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. |
|
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 |
|
Understood. Thanks. |
|
|
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! |
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.