Support » Plugin: Media Library Assistant » Sort Files by filename

  • Resolved cuckoofairweb

    (@cuckoofairweb)


    Hi David, this for you is quick question but how do I sort a group of files by their filename?

    On the page in question have a look at the table “General Reports & Briefings” which has about seven PDFs in it.

    What I want to do is to sort them by their filename and for the life of me I can’t work it out!

    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 short-code for that table currently has:

    [mla_gallery mla_markup=basic-2-column mla_style=basic-2-column category_name=pub-doc tag=”bridgewg-briefings” orderby=”file ASC”]. The markup for basic-2-column contains this:
    post_parent=all, post_mime_type=application/pdf, size=icon_only, columns=1, mla_image_attributes=’width=”16″ height=”16″ ‘, link=none, orderby=”name DESC”. I think the shortcode overrides the basic-2-column setting?

    In the orderby have tried using base_file (I just have one uploads folder!), file_name etc but nothing changes!

    If this isn’ directly possible do I then have to copy over the filename into a another field eg Name and then sort by that – eg set the Name/Slug field to template:[+file_name+]

    Cheers

    Paul

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter cuckoofairweb

    (@cuckoofairweb)

    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

    Thread Starter cuckoofairweb

    (@cuckoofairweb)

    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

    Plugin Author David Lingren

    (@dglingren)

    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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.