• Hi everyone,

    Curious if there is a way, default or otherwise, to return the output of

    [gallery orderby="title"]

    using php’s natsort?

    I often upload images that art titled image1, image2, image3,… all the way up to 100 at times. So when it hits image10, image11, – things start displaying out of order.

    I know that naming them using 001, 002, 003 in advance will alleviate that, but there are times when that can’t be done before upload.

    Natsort will fix it on the server side.

    Any kind of filter available? Thanks.

    • This topic was modified 2 years, 4 months ago by Jan Dembowski.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    Using PHP to sort results isn’t very efficient, it’s much better to let the DB engine do the sorting. Additionally, to use PHP sorting in the gallery shortcode likely involves reproducing a good part of the existing code in your own filter callback — doable, but messy.

    I suggest using the “posts_request” or “posts_orderby” filters to add +0 to whatever ORDER BY column specification that already exists for the gallery query. This forces the DB engine to cast the column values as integers for sorting purposes. Your callback that adds this will need to distinguish a gallery query from all others. This can often be done by checking if certain query vars have certain values unique to the gallery query.

    Thread Starter alex2k5

    (@alex2k5)

    Thanks for the feedback. I get the logic, but it isn’t something I’ve done previously so I’ll have to do some research and testing to see if it works here for this situaiton.

    Thread Starter alex2k5

    (@alex2k5)

    Wanted to update this.

    Spent several hours playing around. Could not get the +0 method to work. Tried with a filter, and also removed the gallery shortcode and rebuilt it in my functions file by copying from media.php, then editing from there.

    Default orderby attribute in default gallery code is:

    'orderby' => 'menu_order ID',

    Replacing it with

    'orderby' => $post->post_title,

    Does sort it by title.

    Replacing it with

    'orderby' => $post->post_title+0,

    Does not, it reverts to upload order/ID.

    So the filter does work, but the +0 does not.

    Tried some variants, using $wpdb global and coding for that, spacing etc. No dice.

    End goal – sort by title with numbers in it – 1,2,3… 11,12,13… 100,101,102 etc. In human readable order.

    Any additional input welcome.

    Moderator bcworkz

    (@bcworkz)

    The +0 is being applied in the wrong place. The “orderby” arg supplied to the query has limited values that are acceptable. To initiate the query (or alter it before it executes), you simply want 'orderby' => 'post_title',. That will get the ordering on the correct column, but not the type of ordering you are after.

    To get the +0 to work, it must be added directly to the actual SQL query code, not WP_Query args. Access to the actual SQL is via the “posts_request” or “posts_orderby” filters. The SQL order by clause will look something like ORDER BY wp_posts.post_title ASC. You want to append +0 to wp_posts.post_title to get ORDER BY wp_posts.post_title+0 ASC.

    Thread Starter alex2k5

    (@alex2k5)

    Ahh, I see. Thanks for the clarification. At some point I dove into filters (don’t use much) and went down the rabbit hole leading me to altering the gallery shortcode. Will dig into your suggestion soon. Again thanks for the time.

    Thread Starter alex2k5

    (@alex2k5)

    Update on this. @bcworkz – appreciate your time and no need to dig further into this as you aren’t my personal coder, but posting for future searches.

    Played with applying the filter to posts_orderby and posts_request. It affected the widgets I had which pulled recent posts before the gallery shortcode was executed within the article (after navbar) AND it affected widgets with recent posts after the article (footer) but not the gallery shortcode within the article/content. So even if I narrowed the filter down to affect only the specific part of the page needed, it wasn’t applying to what was actually needed.

    I see no talk about doing this to gallery shortcode order here or stackexchange or elsewhere on the web other than “you can’t” and variants, even adjacent situations that I could reverse engineer for this. Only for those querying and displaying images or posts in a completely custom format. I need this to apply the the default way galleries are presented within content, with other loops active on page, and retroactively on existing content.

    So not sure if there is a way to suggest things for core changes by a casual person like me, but if anyone reads this – please offer a way to sort galleries via shortcode in human readable numerical order by the title field. Thanks.

    Moderator bcworkz

    (@bcworkz)

    Sorry if I wasn’t more clear. All posts queries go through the suggested filters, so returning altered SQL would affect everything. As you surmised, there needs to be a mechanism to only change gallery queries and let anything else proceed unchanged. Checking for specific characteristic query var values is one way to distinguish queries. Another is to only apply your filter callback just before the gallery query is made. Your callback can then remove itself from the filter stack so it will not effect subsequent queries.

    it wasn’t applying to what was actually needed.

    If you’re willing to share your filter callback code, I can give it a look and see if I notice any problems that would keep it from working.

    If all else fails, you could use the ‘post_gallery’ filter to override the gallery’s functioning and HTML output. This typically entails copying a good part of the gallery_shortcode() function into your callback, then altering it to suit. This still would not help you add a +0 to the SQL unless you replace the get_posts() call with your own custom SQL query using the $wpdb global connection object. However, you could take the return from get_posts() and apply natsort() to the attachment posts.

    It would be a lot simpler if you could get a “posts_orderby” filter to work for you. Successful ordering with the +0 trick partly depends on what the titles actually are. Since the title gets evaluated as an integer this way, you may not get the desired ordering if the alphabetical portion still needs to be a factor in ordering. This could be why your filter callback appeared to not be applied as you wanted.

    Anyone is welcome to suggest thoughtful and meaningful core code enhancements through our Trac system. It’s primarily for bug reports, but enhancement requests are considered as well. Please search existing tickets before posting a new one to be sure a similar ticket does not already exist.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Gallery Shortcode – Sort Return Using Natsort?’ is closed to new replies.