Title: wp_query sorted gallery
Last modified: October 28, 2018

---

# wp_query sorted gallery

 *  Resolved [alx359](https://wordpress.org/support/users/alx359/)
 * (@alx359)
 * [7 years, 6 months ago](https://wordpress.org/support/topic/wp_query-sorted-gallery/)
 * I’m struggling with getting an MLA gallery using custom `wp_query` code properly
   sorted. Figured I need to sort images out using two categories; one’s visible(
   e.g. `prints`), and the other’s hidden and just aids defining their priority (
   e.g. `top`). That way chosen images would get displayed first on top of the others.
   For example:
 *     ```
       $tax_query =
        "array(
           'relation'              => 'OR',
           array(
               'taxonomy'          => 'attachment_category',
               'field'             => 'slug',
               'terms'             => array( 'prints','top' ),
               'orderby'           => 'terms'
               'operator'          => 'IN',
               'include_children'  => true
               ))'";
       ```
   
 * I need the proper `wp_query` syntax to show all the `top` category images first,
   and the `prints` category images second (while excluding anything else).
 * Would you please hint what I need to fix in the snippet above?
 * Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Plugin Author [David Lingren](https://wordpress.org/support/users/dglingren/)
 * (@dglingren)
 * [7 years, 6 months ago](https://wordpress.org/support/topic/wp_query-sorted-gallery/#post-10824551)
 * Thanks for your questions and for including the complete text of your `tax_query`
   parameter; very helpful. You are running up against some limitations of the `
   WP_Query` class and its `tax_query` parameter.
 * The [WP_Query Class orderby parameter](https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters)
   accepts any of a specific list of values on which to sort. These do not include
   any ability to sort by term values assigned to the selected items.
 * The [WP_Query tax_query parameter](https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters)
   does not include `orderby` nor `order` parameters of its own.
 * The `[mla_gallery]` shortcode faithfully follows the capabilities of the WordPress
   core features for `WP_Query`, so I regret that there is no solution or your needs
   using the available features.
 * One of the MLA example plugins, “MLA tax query Example”, demonstrates how to 
   replace the `WP_Query` functions with a custom SQL query. The example was developed
   to increase performance but might be adapted to solve your problem. I have not
   looked at the code for some time and do not know how hard it would be to add 
   something along these lines.
 * You are encouraged to study and modify the example in a way that suits your needs.
   If you come up with a solution it would be great to see it and share it with 
   other MLA users. If you need more specific guidance I may be able to help but
   it will be at least two weeks before I can find some time to do any work of my
   own. Thanks for your understanding and your patience.
 *  Thread Starter [alx359](https://wordpress.org/support/users/alx359/)
 * (@alx359)
 * [7 years, 6 months ago](https://wordpress.org/support/topic/wp_query-sorted-gallery/#post-10825288)
 * Thanks for the insightful explanation, David. It helped me recall that the proper‘
   WP way’ to sort stuff is by the `menu_order` column in `wp_post`. Initial tests
   show such sorting seems already built-in, so no need for any extra `orderby` 
   or sql filters. What I had to do though was to set all `menu_order` rows first
   for all the media other than their default zeros.
 * What I’m still missing is a ‘bulk’ action of editing `menu_order` from within
   MLA, as it isn’t exposed for multiple rows, unfortunately. Would you please advise
   how to do that?
 *  Thread Starter [alx359](https://wordpress.org/support/users/alx359/)
 * (@alx359)
 * [7 years, 6 months ago](https://wordpress.org/support/topic/wp_query-sorted-gallery/#post-10826178)
 * Have found an excellent tool that does the bulk menu_order changes and much more:**
   WordPress Advanced Bulk Edit** by georgeiron. It isn’t free, but quite affordable
   for what it does (bulk editing for most post_types).
 *  Plugin Author [David Lingren](https://wordpress.org/support/users/dglingren/)
 * (@dglingren)
 * [7 years, 6 months ago](https://wordpress.org/support/topic/wp_query-sorted-gallery/#post-10828789)
 * Thanks for your updates with the good news about finding a solution that works
   for your application.
 * It’s true that the MLA Bulk Edit area does not allow for updating the `menu_order`
   field because I couldn’t think of a use case for it. Some time after that I added
   the `[+index+]` substitution parameter for bulk edit that would make some sense.
   Perhaps I will revisit my decision in a future MLA version.
 * In the interim I am marking this topic resolved because your problem has been
   solved, albeit by another plugin. Please update the topic if you have any problems
   or further questions regarding your `menu_order` solution.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘wp_query sorted gallery’ is closed to new replies.

 * ![](https://ps.w.org/media-library-assistant/assets/icon-256x256.png?rev=973502)
 * [Media Library Assistant](https://wordpress.org/plugins/media-library-assistant/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/media-library-assistant/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/media-library-assistant/)
 * [Active Topics](https://wordpress.org/support/plugin/media-library-assistant/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/media-library-assistant/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/media-library-assistant/reviews/)

 * 4 replies
 * 2 participants
 * Last reply from: [David Lingren](https://wordpress.org/support/users/dglingren/)
 * Last activity: [7 years, 6 months ago](https://wordpress.org/support/topic/wp_query-sorted-gallery/#post-10828789)
 * Status: resolved