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. Query to sort a list by meta key first (if it exists), and show remaining posts without meta key ordered by title
  28. How to get custom post meta using REST API
  29. Using get_post_meta with new_to_publish
  30. Add custom fields to search
  31. Meta Query with AND & OR?
  32. Can I count the number of users matching a value in a multiple value key?
  33. WP doesn’t show Array Custom Fields?
  34. How do I search an array stored in a custom-field using WP_Query?
  35. get_pages sort alphabetically by meta value
  36. Get updated meta data after save_post hook
  37. Compare meta_query decimals not working right
  38. WordPress altering my custom query, How to fix it?
  39. How to break meta values into different items and avoid duplicates?
  40. ajax delete value from custom field array
  41. How can I sort get_users() by any value (last_name, user defined fields and more)
  42. How can I query on the year part of a complete date in a custom field?
  43. Transition from (classical) serialized custom meta field to (gutenberg) rest enabled meta
  44. Unable to show ACF’s Image Custom Field properly in Genesis Framework [closed]
  45. Author Page Custom Query WHERE author OR [post meta value] OR [post meta value]
  46. What Is meta_id In wp_postmeta?
  47. How to display Meta Field Value?
  48. How do I use wp_query for WordPress search?
  49. Nav Menu – Add class based on meta keys
  50. Orderby custom field meta value ASC and then by date DESC
  51. Sorting posts ordered by custom field value
  52. Unique Post Meta Values
  53. Create an Array of Specific Custom Post Meta
  54. WP 3.1 meta_query for multiple custom field values
  55. Remove Body Classes
  56. How to create a shortcode to print specific values stored in a post meta array?
  57. Get posts with same meta value as current post
  58. Add custom field to all posts in specific post_type
  59. Change the sort order for posts to include custom field
  60. Button inside Custom Meta Box triggering the Update Button
  61. How to load an assets based on custom field value?
  62. How can you include custom post meta in search without calling each key?
  63. Get Data From wp_sitemeta for Multisite network
  64. How can I do a variable for meta_query?
  65. Can I access a post meta field before the loop?
  66. wrap text around custom fields array
  67. Get all the posts where meta field with multiple choice has several values checked
  68. Hide custom fields when empty
  69. Save all the post tags inside a custom field
  70. ACF: How to get users with a ACF flexible content subfield with a specific value AND layout?
  71. WordPress loop by meta key that is an array? and how loop multiple arrays
  72. How add multiple wp_editor_box to new post
  73. Update post meta custom field using block editor
  74. Get YouTube video id from url in a custom field
  75. Related query shows same image in loop
  76. Can’t Output get_post_meta?
  77. put saved metabox values back into fields and then display on the front end
  78. Displaying an ACF list of users
  79. Meta value Array
  80. How to get custom image field of specific post id
  81. Custom field to array?
  82. How to sort by custom field value?
  83. Custom field outside the loop and inside an array
  84. get_post_custom_values problem, please help
  85. get posts based on non-single metadata
  86. How do i create a custom post query when the meta value is an array?
  87. Woocommerce products search with custom fields
  88. Custom meta fields and meta keys
  89. How to save multiple values with same meta_key, each value linked to another tag id
  90. Saving Custom Field that includes Quotation marks
  91. searching by keywords in post’s metas or pagination links problem
  92. update meta field value after
  93. meta_query ‘compare’ => ‘!=’ not working
  94. Query Posts based on custom field value
  95. Create custom query for search?
  96. Display Data From This Custom Media Upload Meta Box?
  97. Sort custom fields in admin by last field added
  98. Save values generated via API as custom meta fields
  99. Hard Define Custom Field Value
  100. Adding custom field to an array
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
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 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