David,
hi a quick update, I gave up on using a standard field to do the filename sorting as my basic issue was I couldn’t find one.
So I just copied over the filename into a another field eg Name and then sort by that – eg set the Name/Slug field to template:[+file_name+]
Only minor point is that the . between file name and extension is replaced by a – but that doesn’t matter.
Is this the only way?
cheers
Paul
David,
Hi another update… I was puzzled as I use the same basic-2-column style and markup across the site but they all seemed to working ok.
So I had a look and found that for the existing files they all had had their name field populated with the filename. And so the orderby= name worked ok.
So I am now more puzzled as to why for the last couple of days the name field has stopped being auto populated as before this recent turn of events I had not needed to create a mapping rule for the Name field.
I am using WordPress 5.7 running Responsive theme and MLA Version 2.95
Good to hear form you again, and thanks for your question.
You wrote “They all have a filename that begins with YYYY_MM_DD_and then a descriptive title so they should sort ok but they don’t! The last two are out of sequence.” The “February 2021 Downton Village Web Site Web page” document has a file name that violates your naming standard:
2021-03-Downton-Village-website-Bridge-Page.pdf
That explains the immediate problem. Your overall question is “how do I sort a group of files by their filename?” The orderby
shortcode parameter is limited to the database fields listed in the “Order, Orderby” section of the Settings/Media Library Assistant Documentation tab; this is a WordPress restriction. However, one of the options you will find there is the ability to sort on a custom field name. WordPress stores the file name in a hidden custom field you can access for your purpose by adding these parameters to your shortcode:
meta_key="_wp_attached_file" orderby=meta_value order=DESC
Note that this only works because, as you noted “(I just have one uploads folder!)“. If your site divides uploaded files into year/month subfolders the sort order prepends year and month to the file name.
You described using the name/slug standard field for this purpose. WordPress generates name/slug values by “sanitizing” the file name and adding a suffix, if necessary, to make the value unique because it is used for the item’s permalink. I do not advise altering this field unless absolutely necessary. I don’t know why the name field “stopped being auto populated” unless your mapping rule overrode it.
The most general solution would be to create a custom field, e.g., “File Name”, and then define a custom field mapping rule to populate it from a data source such as file_name_
or name_only
. That would avoid the year/month prepending problem on sites configured to add it.
I hope this gives you the solution you need. I am marking this topic resolved but please update it if you have problems or further questions regarding the above suggestions. Thanks for your continued interest in the plugin.