Support » Plugin: NextGEN Gallery - WordPress Gallery Plugin » Problem sorting by numerical filename

  • The sort function is exhibiting unexpected behavior:

    Each gallery I have contains images with numerical filenames, such as 1.jpg, 2.jpg, 3.jpg, etc. I would expect that sorting the gallery by filename (ascending) would list them in numerical order, but it doesn’t. Instead I get 1.jpg, 10.jpg, 11.jpg, [etc], 2.jpg, 20.jpg, 21.jpg…. It seems like it’s sorting each character in the filename separately. Can someone address this?

Viewing 15 replies - 1 through 15 (of 22 total)
  • Mee to …I have the same problem. Can anyone help us?

    WordPress 3.3.2
    NextGen Gallery 1.9.3


    I have the same problem! I tried using a triple digit system to organize 125 thumbnails. Number one being 001, and upwards to 125. BUT, NextGen marches to a different drummer – it doesn’t look at number sequences in the usual way.
    So then I tried an alphabet system. Number one being AAA, number two being AAB, etc. This does not work either!
    I liked NextGen at first, but now I’m having to re-think a solution to listing thumbnails in a pre-set order. Any answers out there?

    I should also order about 1000 images. I hope there are some definitive solutions. I would not like having to rename or move all manually 🙁

    I have the same versions of @seoup

    Running into the same issue and really need a fix for this.

    From a PHP perspective this would be achieved with natcasesort() or just nartsort() (case sensitive and insensitive, respectively) but I’m not sure where this should go.

    This is not the best way of doing it (especially as it’s a hack of the NextGen core files), but it seems to work well enough. You will need PHP 5.3, although it can be reworked for older versions.

    On line 250 of lib/ngg-db.php, after if($result) { add the following code:

    usort($result, function($a, $b) {
        return (strnatcmp($a->filename, $b->filename));

    If you need pre-5.3 support, you can split it out into:

    usort($result, 'natcmp_filenames'); on line 250 and outside of that function, create:

    natcmp_filenames($a, $b) {
        return (strnatcmp($a->filename, $b->filename));

    It’s probably best for both sanity and performance that this be applied to the actual sortorder, so I’m looking into that now.

    Here’s an improved solution that sets the sortorder of each image correctly based on a natural sort of the filenames.

    In the file admin/manage-sort.php, replace the block that starts if (isset ($_POST['updateSortorder'])) with the following:

    if (isset ($_POST['updateSortorder']))  {
                // get variable new sortorder
                $sortGallery = $nggdb->get_gallery($galleryID);
                usort($sortGallery, function($a, $b) {
                        return (strnatcmp($a->filename, $b->filename));
                $sortindex = 1;
                foreach($sortGallery as $image) {
                    $wpdb->query("UPDATE $wpdb->nggpictures SET sortorder = '$sortindex' WHERE pid = $image->pid");
                do_action('ngg_gallery_sort', $galleryID);

    This will take effect if you click “Update Sort Order” on the “Sort gallery” page. It’s still not a perfect solution, but it should be a lot more efficient than the previous one.

    Thanks for the solution, Hephaestus! I, too, was hoping to not have to use a hack, but at least it’s a small one.

    I’m surprised that we haven’t heard from Photocrati on this thread. Do they even monitor their support forum? Seems like it would be easy to incorporate your code into a new release….

    The code above likely breaks the ability to choose what sort order you use as it will always do a natural sort by file name when you click “Update”. It could also be improved by not using get_gallery, but instead getting just the file name and PID for every image in the gallery.

    Plugin Author photocrati


    Hi Hephaestus,

    We’re very sorry to hear about the sort options not working well for you. Please note that we do monitor the forum throughout each week to keep informed about features/settings that are not working for our users, and we actively look for ways we can improve user experience in NextGEN. We do our best to include fixes for bugs found, in the next update or as quickly as possible.

    Could you confirm for me, and anyone else who is experiencing this same issue, the following:

    WordPress up to date?
    NextGEN up to date?

    (please confirm the following, with your server host tech) :
    MySQL up to date (5.2.4)?
    PHP up to date (5.0)?
    PHP Safe Mode OFF?

    and if you’re willing to share:
    Who are you hosted with?
    What theme are you running?
    Which plugins do you have installed and activated?
    Does this error still occur if you switch your theme to the WP default, and deactivate all plugins except NextGEN Gallery? (*be sure to clear your cache and reload your site after deactivating)

    The above information will help us troubleshoot and confirm if this is indeed a bug we need to provide a fix for.

    – Becky

    Hi Becky, thanks for responding. Here are the details of my install:

    WordPress: 3.4.2
    NextGEN: 1.9.7

    MySQL: 5.5.27-cll
    PHP: 5.3.18
    PHP Safe Mode: OFF

    Host is HostGo, theme is custom by me (based on the Bones theme).

    Plugins: Akismet, Contact Form 7, Image Widget, Justified Image Grid, MailChimp, NGG, Print Friendly and PDF, Wordfence, WP Firewall 2, and WP SEO.

    This is a live site and I won’t be switching to a default theme or deactivating other plugins. However, I just noticed I don’t have the sorting issue on my local dev version of the site. I only have NGG and JIG plugins installed locally, and my local mySQL version is 5.5.9, local PHP version is 5.3.10. (Edit to add: Local version is on a MAMP stack.)

    I hope this helps! Please let me know if I can help in any other way to solve this.

    WordPress: 3.4.2
    NextGEN: 1.9.6 and 1.9.7

    MySQL: 5.1.63-0+squeeze1-log
    PHP: 5.3.16
    PHP Safe Mode: OFF

    MySQL: 5.1.63-0+squeeze1-log
    PHP: 5.3.3-7+squeeze14
    PHP Safe Mode: OFF

    Host is OVH and our own internal machines.

    Plugins (bolded in common with ecclescake): Antispam Bee, Contact Form 7, Contact Form 7 Datepicker, Gallery plugin, Google XML Sitemaps, Meteor Slides, NextGEN Facebook OG, W3 Total Cache, Widgets Controller, WordPress SEO, WP-Nicescroll, WP eStore.

    Theme is a child theme of Thematic.

    Hi all,

    I still experience the problem of random sort order, although I have the newest version of NGG. Can perhaps anybody tell me, what the column sortorder means, which is in the table wp_ngg_pictures (bigint)?

    Does this column count all pictures and thus define the sortorder in an gallery?

    Regards CJC

    Have the same problem, the only sort-order that seems to work is “custom”, and clicking the “pre-sort” in there does not work at all.

    Everything is up to date on the server, the theme is a near-vanilla 2012, only other plugins active are relevanssi and Stats



    After dozens and dozens of wasted hours on the forum and trial and error remedies recommended by many, I finally bit the bullet and found a contractor who knows Word Press on “Fivver” and he solved my whole problem by installing “Light Box 2” on my WP blog. This plugin lets you create a gallery right on a page with an option to use photos as “Media” or “Attachments”.

    Once you setup your gallery within a page, all you have to do is click on the highlighted rectangle and then click in the upper left corner unto the little photo icon. That immediately opens your gallery to the “Edit” or “Add” function. And here is the real magic I’ve been waiting so long for: simply click and drag your thumbnail icons to the new position you want on the entire gallery listings, and with a little jiggling in the white spaces between photos, you will see thumbnails jump around a little within the same vicinity. When they do so, just drop your thumbnail into one of the white spaces that opens up and let go! But, you can still click on it again and slide it around a little to trade places with other nearby thumbnails. This action is sometimes slightly quirky, but at least you now have total control over where to locate your thumbnail files! BUT…be sure to keep “inserting” your gallery, and then push “Update” on your page controls to save the order you just arranged. If you don’t insert or update it, you didn’t save it and you’ll have to start all over again. My advice is to experiment by creating only a 9 photo gallery using 3 thumbnails per row. After 5 minutes of clicking and dragging and jiggling the mouse around, you’ll discover the “personality” of this edit program. I have now deleted NextGen from my plugins.

    Thats the standard behavior for wordpress galleries since wp3.5… And sorting the same way works in ngg as well (drag’n’drop every single image using custom sort order in gallery settings), so thats not a solition for me. But, if you have few images and galleries, sure, the standard galleries might work for you.

Viewing 15 replies - 1 through 15 (of 22 total)
  • The topic ‘Problem sorting by numerical filename’ is closed to new replies.