Drupal 7 entity reference field is displaying unpublished content

So I had a view with an exposed filter, to filter the content type by an entity reference field which was referencing another content type. However, it included unpublished items. So I traced the problem back to the content type, which also listed unpublished nodes as options to select for that field.

Entity reference, by default, grabs the most recent state of the entity, whether published or not. So, I needed a workaround, and pieced this together:

Go to the admin “Manage fields” page in your content type for the entity reference field you need to work with. Under “Entity section”, you have the option to select a Views Mode. So using a view will give you more control over the entities that are available.

You’ll need to create the view first. Make sure the view’s display is “Entity Reference” when creating the view, and make sure “Content: Published” is set in the filter criteria.

You may see an error like this: Display “Entity Reference” needs a selected search fields to work properly. See the settings for the Entity Reference list format. If so, go to “Settings” for your Entity Reference list Format, and check an option under Search fields (I’m using Content: Title for mine), and Apply.

Once this view is set up, you can go back to the Node field under Entity Selection on your Manage Fields page, and select the Views mode. The page will then load a “View used to select the entities “ field where you select the specific view.

Now, if you create or edit your content type, you should only see published entities for that entity reference field. And, going back to my original problem, any view where you filter that content type by that entity reference will also only use the published entity reference nodes for filtering.

Leave a Reply