When I try to sort a Gallery created through the [gallery] shortcode by numerical titles I get them sorted in alphabetical order. That means that they are sorted like so:

1, 3, 34, 4, 56, 6, etc.

rather than so:

1, 3, 4, 6, 34, 56, etc.

Doing some research I came across the section of the codex (in WP_Query) explaining this issue. Apparently, setting the orderby parameter to meta_value_num would enable numerical sorting.

Since [gallery] internally uses get_posts() (wp-includes/media.php::714), which in turn uses WP_Query, I thought the same rules would apply.

Changing the order_by attribute in the shortcode to meta_value_num doesn't do the trick. I guess that most likely this is the case because I cannot set up the required meta_key=keyname.

But not only can I not specify the meta key, I don't have one, and I don't need one because I mean to sort them numerically by title.

  • Can you post some code? – jfacemyer Mar 6 '13 at 16:04
  • There is not much code to post, besides [gallery link="file" columns="6" orderby="title"] which gives the alphabetical order and [gallery link="file" columns="6" orderby="meta_value_num"] which, as stated, doesn't do anything. – Sunyatasattva Mar 6 '13 at 16:26

It doesn't look like you can do this with the gallery shortcode, unfortunately.

The Codex doesn't even list meta_value_num as an option (though that doesn't mean it couldn't work.) However, you're correct, you must specify a meta-key to use that ordering scheme with WP_Query.

  • I know the codex doesn't, but doing some reasearch in the source code made me think it should be possible (considering that it uses get_posts()). – Sunyatasattva Mar 8 '13 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.