I have an entity reference field which references and displays multiple nodes on a Drupal 7 install.
I am trying to get them to display in date order (the date is a field on the referenced nodes). The problem I have is that if a referenced node date is changed, or a new one created, the display order doesn't change.

I use a entity reference view to order the nodes on their date fields, which works when you manually edit the master node and then save it. This then updates the order. However this not a very practical solution.

I have tried creating a module which when referenced nodes are modified or created, it programatically loads and saves the master node using node_load and node_save.

However this doesn't trigger the referenced nodes to update their display order.

Does anyone know anything else I can try?

2 Answers 2


In case someone stuck on this, i have a solution for Drupal 8 (you can figure it out for 7):
In themename.theme add a preprocess funtion:

function themename_preprocess_field__field_entity_reference(&$variables) {
    usort($variables['items'], function ($a, $b) {
        $a = isset($a['content']['#node']) ? $a['content']['#node']->field_display_order->value : null;
        $b = isset($b['content']['#node']) ? $b['content']['#node']->field_display_order->value : null;

        // Do the comparison
        // This <=> operator only work for PHP >= 7
        return $a <=> $b;
  1. Go to Structure > Content types and select your content type.
  2. Click the "Manage Fields" tab.
  3. Click edit next to your entity reference field.

There should be a "FIELD SETTINGS" area with an "ENTITY SELECTION" box. At the bottom of the "ENTITY SELECTION" box, there's a "Sort by" drop-down menu that should let you select "A field attached to this entity." That should give you access to the date field that you want to use as your sort value.

  • Thanks for the suggestion, I have tried that. It sorts the content perfectly in the content edit area. But doesn't change the order when you render the field.
    – inertiahz
    Feb 18, 2018 at 13:35
  • It looks like this might help: Display order of entity reference fields
    – sjhuskey
    Feb 19, 2018 at 2:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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