Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

change order of images attached to post

Answer Version 2

I apologize for the confusion.

Here’s how you can use the usort() function to sort the images (in the div#pic-con on the front-end):

<div id="pic-con">
<?php
    if (!empty($vid_pix)) {
        usort( $vid_pix, function( $a, $b ){
            $aPor = (int) get_post_meta( $a, 'photo_order', true );
            $bPor = (int) get_post_meta( $b, 'photo_order', true );

            if ( $aPor === $bPor ) {
                return 0;
            }

            return ( $aPor < $bPor ) ? -1 : 1;
        } );

        foreach ($vid_pix as $vP) {
            // echo `div.photo-upload-box` here
        }
    }
?>
</div>

Related Posts:

  1. Orderby meta_value only returns posts that have existing meta_key
  2. ORDER BY custom field value
  3. Can I query custom meta data through WP_Query
  4. When using add_post_meta and update_post_meta, is there any way to give the individual arrays keys?
  5. Auto sort the wp-admin post list by a meta key
  6. Is it possible to store arrays in a custom field?
  7. Ordering posts by anniversary using only day and month
  8. Displaying posts with only upcoming dates according their custom field date value
  9. Sorting posts by multiple values, combined
  10. Which is best in the following scenario : post_meta vs custom table vs parent/child posts
  11. Get specific custom field keys from a post and put into an array
  12. How to add a new meta key and assign timestamp to posts
  13. Run a check for multiple meta key values
  14. How do I have WP_Query match posts based on search parameter OR meta fields? (rather than search parameters AND meta fields)?
  15. Sort alphabetically by custom field
  16. Ordering posts by custom field named “date” in backend
  17. Unique meta_key with array value vs repeated meta_key with single values
  18. Avoiding ACF get_field and returning to core WordPress function
  19. ORDER BY custom field value out of where clause
  20. wordpress custom loop ascending descending posts by custom field
  21. How to improve my non-unique metadata MySQL entries?
  22. Trouble with serialized metadata
  23. How do i output images from URL’s added to the same custom field key
  24. Build Array from Input Fields question
  25. Order by a meta field in query loop
  26. WP Query Args – search by meta_key or title
  27. SQL query based on two different custom field values
  28. Using meta query (‘meta_query’) with a search query (‘s’)
  29. Can I exclude a post by meta key using pre_get_posts function?
  30. Query to sort a list by meta key first (if it exists), and show remaining posts without meta key ordered by title
  31. Custom post meta field effect on the performance on the post
  32. How to get custom post meta using REST API
  33. Difference between meta keys with _ and without _ [duplicate]
  34. What is the index [0] for on post meta fields?
  35. Using meta_query, how can i filter by a custom field and order by another one?
  36. What is “meta_input” parameter in wp_insert_post() used for?
  37. How to enable revisions for post meta data?
  38. The “_encloseme” Meta-Key Conundrum
  39. Best way to programmatically remove a category/term from a post
  40. Using get_post_meta with new_to_publish
  41. Add custom fields to search
  42. SELECT max(meta_value) FROM wp_postmeta WHERE meta_key=’price’… stops working when value is over 999
  43. Matching Serialized Arrays with meta_query
  44. Custom field metabox not showing in back-end
  45. Meta Query with AND & OR?
  46. So much data in postmeta
  47. Can I count the number of users matching a value in a multiple value key?
  48. Getting attachments by meta value
  49. How to hide meta box values from custom fields list?
  50. WP doesn’t show Array Custom Fields?
  51. get_post_meta() unserialize issue – returns boolean(false)
  52. What is the advantage of the wp_options design pattern?
  53. Storing meta fields multiple times OR once with multi dimensional array?
  54. Allow user to create instances of custom field
  55. display specific custom fields
  56. How do I search an array stored in a custom-field using WP_Query?
  57. How Can I save multiple records in same meta key?
  58. Is there a hook / action that is triggered when adding or removing a post thumbnail?
  59. get_pages sort alphabetically by meta value
  60. Meta keywords and descriptions plugin for manually editing meta for each page/post
  61. passing argument to get_template_part() or a better way to code
  62. Archive sorting functions by custom fields (front-end)
  63. Get updated meta data after save_post hook
  64. Multiple meta values for same meta_key adding on “Preview Changes” hit but not on saving or updating post
  65. Save HTML formatted data to post meta using add_post_meta()
  66. Trying to perform complex custom field query with order by set to field value
  67. importing data from non-wordpress mysql db
  68. How to update single value in multi dimensional Post Meta?
  69. Is there a way to do multiple ordering on a multiple meta_query?
  70. Order by custom field date?
  71. Create meta boxes that don’t show in custom fields
  72. Transients vs CRON +Custom Fields: Caching Data Per Post
  73. Unable to save datetime custom meta field using update_post_meta() function
  74. Up/Down voting system for WordPress
  75. post meta data clearing on autosave
  76. WordPress Search Custom Meta Field Only
  77. Create custom field on post draft or publish?
  78. Display info from custom fields in all images’ HTML
  79. Compare meta_query decimals not working right
  80. WordPress altering my custom query, How to fix it?
  81. WP Meta Query for some meta (array) values
  82. Calling Specific Pages with wp query Part II
  83. Loop through two different sets of custom fields
  84. get_post_meta fields don’t show up on posts page
  85. Update meta values with AJAX
  86. How to break meta values into different items and avoid duplicates?
  87. use get_posts to get custom field data, but in one array
  88. copy attachments to another post type and change attachment url
  89. Cannot edit post meta fields with rest API
  90. ajax delete value from custom field array
  91. Save attachment custom fields on front end
  92. How can I sort get_users() by any value (last_name, user defined fields and more)
  93. Slow meta query with multi meta keys
  94. How to use pagination with get_post_meta
  95. Copying Custom Meta Values from existing post to a duplicate post
  96. Using OR in WP_Query negates the “NOT EXISTS” compare
  97. Add a post meta key and value only if it does not exist on the post
  98. How can I query on the year part of a complete date in a custom field?
  99. Saving an array in a single custom field
  100. Move value of one custom field to another
Categories custom-field Tags array, custom-field, meta-query, post-meta, sort
Related post based on content
it’s possible make a WP_Query with math operations?

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress