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

How can I sort homepage by a meta value?

You probably also need:

$query->set( 'ignore_sticky_posts', true );

That will prevent sticky posts from shuffling to the top.

Maybe:

$query->set( 'posts_per_page', -1 );

If you are getting too few posts. -1 means “all”. You can use that to change to a number other than what is set in the backend.

A couple of notes:

  1. $query->set( 'meta_key', 'key name' ); will limit the results to
    posts with that meta_key
  2. If your meta_value for the key name isn’t really a number the
    order isn’t going to work correctly. Punctuation and letters will mess this up.
  3. is_home can be peculiar. Make sure it does what you need it
    to do.

Beyond that, you will need to explain “not returning all the posts and not ordering them properly” in better detail.

Related Posts:

  1. Author Page Custom Query WHERE author OR [post meta value] OR [post meta value]
  2. How do I query for a post by custom field?
  3. using multiple meta_key and meta_value in query_posts
  4. meta_compare seems to be treating values as strings instead of integers as expected
  5. Limits, not all post are showen when querying for posts by view count
  6. Unable to get specific value from post meta
  7. Custom search SQL Query to add custom field in result
  8. Custom Query: query by post custom meta data
  9. Get author total post votes from post meta
  10. searching by keywords in post’s metas or pagination links problem
  11. Custom query with orderby meta_value of custom field
  12. what is the correct way to compare dates in a WP query_posts meta_query
  13. Can I exclude a post by meta key using pre_get_posts function?
  14. Custom post meta field effect on the performance on the post
  15. How to get custom post meta using REST API
  16. Difference between meta keys with _ and without _ [duplicate]
  17. Orderby meta_value only returns posts that have existing meta_key
  18. What is the index [0] for on post meta fields?
  19. What is “meta_input” parameter in wp_insert_post() used for?
  20. Query Posts or Get Posts by custom fields, possible?
  21. How to enable revisions for post meta data?
  22. The “_encloseme” Meta-Key Conundrum
  23. Best way to programmatically remove a category/term from a post
  24. Using get_post_meta with new_to_publish
  25. How to rename a custom field?
  26. How can I make wp-pagenavi work on a custom query built upon a form submission? [closed]
  27. Custom field metabox not showing in back-end
  28. Meta Query with AND & OR?
  29. So much data in postmeta
  30. Can I count the number of users matching a value in a multiple value key?
  31. When using add_post_meta and update_post_meta, is there any way to give the individual arrays keys?
  32. How to hide meta box values from custom fields list?
  33. Auto sort the wp-admin post list by a meta key
  34. get_post_meta() unserialize issue – returns boolean(false)
  35. What is the advantage of the wp_options design pattern?
  36. Storing meta fields multiple times OR once with multi dimensional array?
  37. Allow user to create instances of custom field
  38. Query Custom Meta Value with Increment
  39. query_posts and only show results if a custom field is not empty
  40. display specific custom fields
  41. Filter archive.php by custom meta
  42. Is there a hook / action that is triggered when adding or removing a post thumbnail?
  43. Meta keywords and descriptions plugin for manually editing meta for each page/post
  44. passing argument to get_template_part() or a better way to code
  45. Is it possible to store arrays in a custom field?
  46. Get updated meta data after save_post hook
  47. Multiple meta values for same meta_key adding on “Preview Changes” hit but not on saving or updating post
  48. Save HTML formatted data to post meta using add_post_meta()
  49. importing data from non-wordpress mysql db
  50. Order posts by custom field and if custom field is empty return remaining posts
  51. Create meta boxes that don’t show in custom fields
  52. Transients vs CRON +Custom Fields: Caching Data Per Post
  53. Unable to save datetime custom meta field using update_post_meta() function
  54. Up/Down voting system for WordPress
  55. post meta data clearing on autosave
  56. Create custom field on post draft or publish?
  57. Display info from custom fields in all images’ HTML
  58. Ordering posts by anniversary using only day and month
  59. get_post_meta fields don’t show up on posts page
  60. Update meta values with AJAX
  61. Query on custom field count?
  62. How to break meta values into different items and avoid duplicates?
  63. copy attachments to another post type and change attachment url
  64. Cannot edit post meta fields with rest API
  65. ajax delete value from custom field array
  66. Save attachment custom fields on front end
  67. How to display liked posts of current user in wordpress?
  68. How to use pagination with get_post_meta
  69. Copying Custom Meta Values from existing post to a duplicate post
  70. How to use query_posts() with a date filter on a custom field?
  71. Add a post meta key and value only if it does not exist on the post
  72. Move value of one custom field to another
  73. Order posts according to user defined order for meta values?
  74. Displaying posts with only upcoming dates according their custom field date value
  75. Custom fields to save multiple values
  76. Custom fields: In what order are they saved into the DB?
  77. Function to change meta value in database for each post
  78. Get a post_id where meta_value equals something in a serialized meta_value field
  79. Get aggregate list of all custom fields for entire blog
  80. Plugin similar to Taxonomy Drill-Down for custom fields?
  81. Transition from (classical) serialized custom meta field to (gutenberg) rest enabled meta
  82. Unable to show ACF’s Image Custom Field properly in Genesis Framework [closed]
  83. Custom field value based on other custom field values
  84. meta_value_num sort glitch
  85. Can ordering post list by meta_value cause performance issue?
  86. Order by value in serialized custom field
  87. wp_handle_upload error “Specified file failed upload test” but still creates attachment?
  88. How to save a ToggleControl value in a meta field?
  89. Which is best in the following scenario : post_meta vs custom table vs parent/child posts
  90. Saving custom image meta fields
  91. meta query not showing any results?
  92. How to display Meta Field Value?
  93. MySQL query to set wp_postmeta using term_taxonomy_id value
  94. Count how many posts have a custom field set
  95. How to Validate Post Meta type/extension (Video File Image File etc)
  96. Get all meta keys assigned to a post type
  97. Upcoming Event: How do I sort database by custom date field, but ignore past dates?
  98. how can i use custom field in query post
  99. Custom Meta Box not Saving in Posts with Gutenberg Editor
  100. sort search results by custom fields using dropdown
Categories custom-field Tags custom-field, post-meta, query, query-posts
Integrate WordPress in Yii
WP_Error not displaying errors

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