2

I would like to get the last string of a entry title and order entries based on that. Basically the people title are their full names and I am trying to find their last name and filter based on that, instead of their first name.

{% set entries = craft.entries.section('people').order('title').limit(0) %}

similar to this entry.title|split(' ')|last

3

It’s not possible to do that unless you’ve already got the last word saved somewhere.

Here’s what I would recommend:

  • Create two new Plain Text fields - “First Name” and “Last Name”
  • Go to your People section’s entry type settings
  • Uncheck the “Show the Title field?” setting and set the “Title Format” setting to:

    {firstName} {lastName}
    
  • Add the “First Name” and “Last Name” fields to the entry type’s field layout

  • Save the entry type
  • Update your existing People entries

With those changes made, you will now be able to order directly by your Last Name field’s values:

{% set entries = craft.entries.section('people').order('lastName asc').limit(0) %}
|improve this answer|||||
  • Oh ok, ya unfortunately these titles we're imported from wordpress so the title tags were already populated and we have 122 people so redoing them all is too big of a task. Thanks anyway! – Kenyon Kowal Oct 9 '15 at 0:08
  • @KenyonKowal Gotcha. Just added a new answer that should work for you. – Brandon Kelly Oct 9 '15 at 0:21
4

Another option is to use the Preparse plugin to automatically parse out and store last names in a hidden field.

  • Install the Preparse plugin
  • Create a new “Last Name” field using the Preparse field type
  • Set the field’s Twig template setting to this:

    {{ entry.title|split(' ')|last }}
    
  • Edit the People section’s entry type settings, and add the Last Name field to its field layout.

  • Edit the People section’s main settings - just re-save them, so that all of your existing entries get updated.

Now when your People entries are saved, the last word in their Title will be copied over to a hidden “Last Name” field, and you can sort by that in your template:

{% set entries = craft.entries.section('people').order('lastName asc').limit(0) %}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.