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

WordPress loop by meta key that is an array? and how loop multiple arrays

You can’t order by values inside serialised arrays in post meta. When you store an array like this:

$value = [
    [
            'plan_name'  => 'name 1',
            'plan_price' => '1',
            'theType'    => 'yesPaid',
            'plan_space' => 'Unlimited',
    ],
    [
            'plan_name'  => 'name 2',
            'plan_price' => '3',
            'theType'    => 'yesFree',
            'plan_space' => '5000',
    ],
    [
            'plan_name'  => 'name2',
            'plan_price' => '5',
            'theType'    => 'yesPaid',
            'plan_space' => 'Unlimited',
    ],
]

This is stored in PHP like this:

a:3:{i:0;a:4:{s:9:"plan_name";s:6:"name 1";s:10:"plan_price";s:1:"1";s:7:"theType";s:7:"yesPaid";s:10:"plan_space";s:9:"Unlimited";}i:1;a:4:{s:9:"plan_name";s:6:"name 2";s:10:"plan_price";s:1:"3";s:7:"theType";s:7:"yesFree";s:10:"plan_space";s:4:"5000";}i:2;a:4:{s:9:"plan_name";s:5:"name2";s:10:"plan_price";s:1:"5";s:7:"theType";s:7:"yesPaid";s:10:"plan_space";s:9:"Unlimited";}}

As far as MySQL — which is responsible for querying posts — is concerned, this is just a long string. Indistinguihsable from a word or sentence that begins with a. It’s only turned back into an array by WordPress when the value is retrieved with get_post_meta().

So it’s not possible to sort by a property within an array, and if you attempt to sort by the meta value of this meta then it’s just going to sort as if this were a string.

If you need to sort this way then you need to find an alternate way of storing the data. I don’t have enough insight into your project to suggest anything specific, but you probably need a custom database table for these plans, which you’d then need to join and sort using the posts_join and posts_orderby hooks to modify the query SQL directly.

Related Posts:

  1. Order a WP_Query by meta value where the value is an array
  2. WP post meta – for loop inside for loop
  3. How to update custom fields using the wp_insert_post() function?
  4. How can merge two arrays values in one array and save in database
  5. How to display multiple Post meta_key/meta_values by SQL query
  6. How to use multiple query with same meta key
  7. How to update single value in multi dimensional Post Meta?
  8. How to loop over custom fields in a page template?
  9. Print html when custom field has value inside while loop
  10. Compare two meta key values against each other inside the get_posts array?
  11. How can update custom meta for all posts
  12. Looping through WP_Post Object
  13. Use ajax to update_post_meta
  14. how to increase custom post value by one most efficiently?
  15. Must Use Plugin Causing Query Error
  16. Create a random unique 6 digit number as custom field for custom post type
  17. How can I add multiple ‘tax_query’ arrays via a loop?
  18. How can I create a list of page titles from custom meta values?
  19. Store multiple custom field as post meta per post(css, js, html, 2 link) [closed]
  20. How to VAR_DUMP a $variable during checkout process (Is my product meta callable?)
  21. How can I add extra word in permalink when someone click download button?
  22. Checking array against author id in loop
  23. Advanced Custom Fields – display label and value only if value entered
  24. Is it possible to retrieve all posts with a certain value for metadata?
  25. Get post id within comments loop
  26. Non-Closing PHP Query in WordPress Loop
  27. Output meta into arrays
  28. how to get serialized post meta
  29. How to get the last category name of a child category?
  30. How to use two meta_compare in an array?
  31. Show ACF field with link to ultimate member profile/WordPress user profile below the post (single post layout)
  32. Remove empty terms from array, sort alphabetically, update back to repeating field
  33. Only show first image in foreach loop
  34. Get all posts as an array ID => Name
  35. How to store multiple custom meta box
  36. How to add post meta in while loop?
  37. Add a custom class to the body tag using custom fields
  38. Define category ID using get_post_meta
  39. Hide a div when a custom field is empty
  40. Update value of a associative array with update_post_meta
  41. WordPress stripping out custom field tags
  42. Add custom fields from different posts
  43. Order category posts by last word of custom field
  44. Add a “custom field” to a category that can be retrieved when viewing the category page with get_post_meta
  45. Products listing check if meta checkbox is checked
  46. in_array not working on dev server but works on localhost
  47. How to update custom fields when post is published?
  48. Where does php code to load data go?
  49. ACF – Get ID of relationship field in flexible content
  50. Show the subcategory name based on current product
  51. Store custom field’s multiple values in one user meta key
  52. Remove item from post_meta array via AJAX
  53. When working with a post, almost all wp_postmeta are deleted
  54. Updating Lat and Lng of posts automatically gives sporadic results
  55. Efficient way of querying for a “fallback” post?
  56. WP_Post is not from correct array
  57. divide custom field values in div every two values
  58. Create Customization Controls from Array
  59. If custom field doesn’t exist, or exists and it’s true show title?
  60. How can I put a custom field as the link of a button shortcode?
  61. Metadata on the WP_Post object
  62. Grabbing value of input field inside of array
  63. How to initialise WP_Query on the basis of a specific meta_value and continue iterating rest?
  64. I want to show image from custom field image on my custom page template
  65. fetch from an external api call and display results in page
  66. How to pull through page title depending on which radio button is selected on the specific page?
  67. Unable to Call More than One Meta Box Output
  68. Create Div Wrapper On 3rd Iteration Or When Query Loop Items Run Out
  69. How to Fix an Archive.php That Displays All Posts?
  70. WordPress loop is not working
  71. Echo array value
  72. Including Custom fields within the_content
  73. How can i make a search box to search by custom field
  74. List all anchor links on a page
  75. Php code error in wordpress if else statements [closed]
  76. First post article different on Archives template
  77. Front end file upload returning wrong attachment url
  78. How to add title attribute to archive items
  79. How to add ASCII symbol after each recent post [closed]
  80. Comparing Dates from custom field
  81. Conditional On custom field plugin metabox
  82. Show field if it has contents on Advanced Custom Fields
  83. How to make sure relative URL works when site is not on root domain?
  84. Get URI from PodPress using PHP
  85. Adding custom url to readmore link using get_permalink()
  86. pagination broken – clicking next displays “page not found”
  87. Filter unique custom field value based on custom taxonomy category or other custom field
  88. Post meta select input, if statement
  89. How To Pass Array To get_the_category_by_ID() and Get An Array Back?
  90. using images in next/previous_post_link [closed]
  91. the_meta – no get_the_meta query?
  92. Display movies that apear this day next
  93. Strip from or something better?
  94. Add “alt” Attribute to GD Star Rating Tags
  95. How to break up php code to avoid echo
  96. Display background color or image with custom meta box?
  97. Finding and removing duplicates within WP Arrays
  98. Display Custom Field in Sidebar if Value is Present
  99. custom header text
  100. Using multiple variables to assign categories to an array
Categories PHP Tags array, custom-field, loop, php, post-meta
Add term slug in URL of custom post type details page
Password not resetting on wordpress?

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