Displaying dates in the document library
Document Library Pro includes several options for how dates are displayed and formatted in your document libraries. For example, you can tell the plugin which fields are dates so that it will sort by date rather than alphabetically.
date_fields option is used to specify which fields in your document library should be treated as dates. This is useful if you have custom fields or taxonomies which represent date values, and you want enable sorting for these fields or change the date format. This option should contain one or more fields (as a comma-separated list) using the same syntax as the
fields option. For example:
[doc_library fields="image,title,cf:date,tax:custom_tax" date_fields="cf:date,tax:custom_tax"]
Setting this option will also allow you to set the date format for these fields using
Should I use this for the
date field or ACF date picker fields?
If you are using the built-in
date field, or date picker fields created with Advanced Custom Fields, then you don't need to use the
date_fields option. Document Library Pro already knows these fields are dates and handles them appropriately.
date_fields is only needed for additional date custom fields, for example:
- You're using a standard WordPress custom field as a date
- You're using an ACF text field as a date
- You're using another custom fields plugin not supported by Document Library Pro
Date sorting isn't working
If you're trying to sort by date custom field or taxonomy, you need to make sure you included the field in the
date_fields option. For example, if you have a custom field called
event_date, then your document library shortcode might be:
[doc_library fields="title,cf:event_date" date_fields="cf:event_date" sort_by="cf:event_date"]
If your field still isn't sorting correctly, it's probably because your dates are stored in European or Australian format in the WordPress database, i.e. day before month (d/m/y).
If this applies to your site, you will need to force the document library to use EU/AU dates. The simplest way to do this is to install our helper plugin which forces all date custom fields and taxonomies in your document library to be "day first" rather than "month first". You can download the helper plugin here:
Install this plugin by uploading the zip file in the usual way from your WordPress Dashboard.
You can also force dates to be in EU/AU format on a per-field or per-taxonomy basis, by adding some code to your theme. The relevant hooks are:
date_format option tells the document library how to format the
date field in the document library. It only works if your document library includes a date field. If you don’t set this option, the default format in your WordPress settings will be used (see Date Format under the Settings → General menu).
You can set the date format to any valid date/time format string. For example:
F j, Y will format the dates in this format: “August 24, 2016”. See the WordPress Codex for more information.
[doc_library fields="title,date" date_format="F j, Y"]
The date format isn't working for my custom field
If the date format is wrong despite using the
date_format options in your shortcode, then the plugin where the date custom field is managed/stored is probably overriding the date format.
To fix this, go to the settings for your custom field and look for a date format option. You should probably remove the
date_format option from your Document Library Pro shortcode as this might cause conflicts with the date format in the 3rd party plugin.
If your custom field has been provided by an events plugin, you will need to check the date format settings for that plugin. For example, if you're using Document Library Pro to display a date field in the Events Calendar plugin then you can find these settings at Events → Settings → Display.