NextGEN Gallery
Problem sorting by numerical filename (23 posts)

  1. ecclescake
    Posted 3 years ago #

    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?


  2. seoup
    Posted 3 years ago #

    Mee to ...I have the same problem. Can anyone help us?

    WordPress 3.3.2
    NextGen Gallery 1.9.3

  3. info@ebrightentertainment.com
    Posted 3 years ago #

    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?

  4. dupardipal
    Posted 3 years ago #

    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

  5. Hephaestus
    Posted 3 years ago #

    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.

  6. Hephaestus
    Posted 3 years ago #

    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.

  7. Hephaestus
    Posted 3 years ago #

    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.

  8. ecclescake
    Posted 3 years ago #

    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....

  9. Hephaestus
    Posted 3 years ago #

    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.

  10. photocrati
    Plugin Author

    Posted 3 years ago #

    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

  11. ecclescake
    Posted 3 years ago #

    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.

  12. Hephaestus
    Posted 3 years ago #

    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.

  13. caiusjuliuscaesar
    Posted 3 years ago #

    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

  14. torsteino
    Posted 3 years ago #

    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 WordPress.com Stats

  15. info@ebrightentertainment.com
    Posted 3 years ago #


    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.

  16. torsteino
    Posted 3 years ago #

    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.

  17. info@ebrightentertainment.com
    Posted 3 years ago #

    In response to tosteino's comment, let me clarify:
    I am now using Light Box 2 which is not the same as "Standard Galleries". The enlarged pages are beautiful compared to all the junk and writing and garbage on the standard WP galleries.
    One of my galleries is 115 photos long! My other 5 galleries range from 30 to 60 photos each. I was able to sort all the thumbnails in very quick order - a matter of about 4 hours. Compare THAT to all the time everyone is wasting on NextGen or other programs. Open your mind and give a hundred photos a try...you'll see what I mean. Just thought I would try to help rescue all the others that are as frustrated with the UNFINISHED software design of NextGen.

  18. torsteino
    Posted 3 years ago #

    First of all:

    There is no plugin on the WP repository called only "Light Box 2", but there is this one: http://wordpress.org/extend/plugins/wp-lightbox-2/, is it that one you are talking about?

    If it is not that one, please give me a link, I'm always interested in new solutions.

    However, if it IS that one, it is definately just using the standard gallery-admin tool, the only thing it changes is to add a lightbox-effect (colorbox) to the presentation. Try to deactivate it if you don't believe me.

    But yeah, the new gallerysolution from 3.5 and up is quite nice. I'm using it on several sites already.

    For relatively few galleries, even when they are a bit larger, as you have, I'm sure ordering everything manually works fine. However, when some of my clients have a few thousand pictures in 50+ galleries, and one of my own sites - 120+ galleries with 10-30 pictures in each, ordering everything manually is not going to cut it.

    Get it now? Some of us really need automatic ordering of the pictures.

  19. photocrati
    Plugin Author

    Posted 3 years ago #

    Hey everyone,

    Would you all mind downloading and installing our most recent Beta (found here) and let us know if this issue persists or if its resolved?

    If the issue persists for you, please submit your bug report here, with temp. login credentials so we can dig deeper into why this is happening for some users but not all. Also, please include in your bug report if the issue persists with all plugins deactivated except NextGEN Gallery > browser cache cleared and site refreshed. Also, try switching to WordPress Default Theme. One other thing to try is disabling all browser plugins/extensions.


  20. supervinnie40
    Posted 3 years ago #

    Another victim here.

    I came his via Google.
    I downloaded the BETA, but the problem has only partly solved.

    I've got a gallery with almost 1000 photo's in it. They are sorted like this:
    up till

    But weirdly enough, it jumps from 21314 to 2815.
    So half way in, the beginning begins... it start around the middle (with 21000).

    Quite annoying.

    Latest version of wordpress (just installed it).
    No other plugins (really, none).
    No browser plugins.
    Beta version of NEXTgen Gallery.

  21. supervinnie40
    Posted 3 years ago #

    Just discovered a second situation with the plugin.

    I just made another gallery.
    This time the files are ordered like this:
    up to
    (the 1 is a sign that it's part 1 of a larger set of gallery's.
    But instead of just sorting the numbers it gives me this:


    Basicaly, it making a big mess of my files.
    Again: quite annoying.

    Is it really that hard to make a script that can just read filenames and sort them? I mean, you guys can make an automated gallery system with integrated styles etc. But you can't even make it sort filesnames correctly.

  22. photocrati
    Plugin Author

    Posted 3 years ago #

    @supervinnie40 , thank you for your feedback, if you wouldn't mind submitting your beta results in a bug report here, we'll take a closer look at this and hopefully be able to provide a fix in the next beta/upgrade.


  23. supervinnie40
    Posted 3 years ago #


Topic Closed

This topic has been closed to new replies.

About this Plugin

  • NextGEN Gallery
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic