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

Ordering Posts By Meta Data

First: don’t use query_posts. It will confuse the main WordPress loop.

You will need two queries. First use the query you have already constructed to get the priority posts:

$query1 = new WP_Query( $args ); // here use the $args you already have

Now count how many posts are returned to set the maximum return on the second query:

$count = $query1->post_count;
$args = array ('posts_per_page'=>15-$count) // and any other arguments you may want to parse
$query2 = new WP_Query( $args );

Now loop through both queries to display them. Note that if you need pagination you will have to do some more clever stuff to make sure you get the right posts and amount of posts on your page.

Related Posts:

  1. get_post_meta causes database queries
  2. Order by meta value or date?
  3. Filtering posts by post meta data
  4. How to query_posts using meta_query to orderby meta_key AND have a secondary sort by date?
  5. Custom query with query_posts doesn’t show post without certain meta_key
  6. How to sort by meta value?
  7. How to display liked posts of current user in wordpress?
  8. Using database meta_values to calculate new post order using pre_get_posts or a ‘request’ hook
  9. Order by meta_key with two meta_queries
  10. Sort Posts by Multiple Meta Values [duplicate]
  11. Querying posts with meta value that begins with a certain pattern
  12. ACF Custom Field WP_Query, but need to get all posts, if field doesn’t exist
  13. IF.. post meta show… Conditional Tag Help…?
  14. Single meta key with multiple meta value in like comparison
  15. Get posts between custom dates
  16. order posts by meta value on posts page
  17. Query_post($args)
  18. query_posts() on key’s value, or key’s existence
  19. Fetch Record based on meta key dates
  20. how to make members list directory through wordpress post custom meta key.
  21. How to retrieve an array of post IDs by a particular value stored in a custom meta’s array
  22. Query posts by meta_key whose value is an array
  23. Filter Loop by Custom Field Value
  24. meta_query check for meta value in key which holds an array of values
  25. Exclude posts by post meta value
  26. Frontend form with multiple posts
  27. query_posts orderby postmeta [closed]
  28. Meta query stopped working
  29. Custom query looking at multiple custom fields and properly sorting
  30. How do i create a custom post query when the meta value is an array?
  31. Tax query get first product with attribute value in pre_get_posts
  32. Use WP_query to match post types based on custom field values
  33. query_posts() with multiple meta data comparisons
  34. get all posts with certain meta data
  35. Limiting the amount of posts this displays
  36. How to Get All Posts with any post status?
  37. posts_per_page no limit
  38. what is the correct way to compare dates in a WP query_posts meta_query
  39. Nested meta_query with multiple relation keys
  40. How can i get count from query post
  41. query_post by title?
  42. Why query_posts() isn’t marked as deprecated?
  43. Alternative to query_posts for main loop? [duplicate]
  44. how to query posts by category and tag?
  45. Using WP_Query to Query Multiple Categories with Limited Posts Per Category?
  46. Use REGEXP in WP_Query meta_query key
  47. Using meta_query, how can i filter by a custom field and order by another one?
  48. How do I query by post format in WordPress 3.1
  49. Display posts of the last 7 days
  50. Is there a way to exclude the content from the post variable to save on RAM usage?
  51. Query posts by custom taxonomy ID
  52. Fail to compare dates in meta_query
  53. Display/query post formats
  54. How to query post by user role?
  55. How to return results of a get_posts() in explicitly defined order
  56. Help to condense/optimize some working code
  57. How to set posts per page using WP_Query()
  58. query_posts exclude a meta key
  59. How do I create a random post that will last for a day
  60. Ensuring sticky posts are retrieved first (without using two queries)?
  61. Meta compare with date (stored as string) not working
  62. Getting attachments by meta value
  63. Query Multiple Post types each with own meta query
  64. Determine if more posts are available than was asked for in `query_posts()`?
  65. Modify main WordPress loop with a parse_query filter
  66. Ordering posts having multiple post-meta date fields
  67. query_posts() in function makes global $wp_query out of sync?
  68. Limiting query_posts to 1, regardless of sticky post?
  69. How to make “sticky” pages (and query by them)
  70. Custom Queries: Joining On Meta Values From Two Custom Post Types
  71. Query posts: how to exclude results if post is in multiple categories
  72. Query Custom Meta Value with Increment
  73. query_posts ->using meta_compare / where meta value is smaller or greater or equals
  74. Problem with ‘post__not_in’
  75. How to order posts by descending comment count on taxonomy page?
  76. How Can I save multiple records in same meta key?
  77. Sort posts alphabetically by custom field value, insert divider between different letters
  78. Is it better practice to use query_posts, WP_Query, or get_posts to create various custom loops within a Page?
  79. WP 5.8 “Query Loop” block: where to place custom query?
  80. Perform query with meta_value date
  81. Sort X categories by last update and show image
  82. Custom Post Type “Event”: chronological list of recurring events
  83. Meta query interfering with orderby relevance
  84. WooCommerce conditional meta query
  85. Meta Query with date and time on the same Day before given time
  86. using post__in allow duplicate post id
  87. Pagination on archive.php page
  88. WordPress Custom Query
  89. Best way to load page content in Fancybox popup?
  90. How to update single value in multi dimensional Post Meta?
  91. What is an efficient way to query based on post_meta?
  92. search query within custom taxonomy term, post title and meta field
  93. How should I intercept the main query and inject custom join / order by / group by criteria
  94. advice on creating a ‘related posts’ query like the one used on stackexchange
  95. order post my meta value m/d/y format with year as included value
  96. Order by meta value, pro first, then free
  97. Having trouble generating pagination links on custom query
  98. WP_Query not working as expected for attachments and custom meta_query
  99. I can’t set meta_key in my custom post type query
  100. How to create an attachments archive with working pagination?
Categories query-posts Tags meta-query, post-meta, query-posts
What to do to have access to WordPress functions
Custom Taxonomy – Dropdown

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