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

Is it possible to sort the post based on a custom field?

Yes,
You can call action “pre_get_posts”.

add_action('pre_get_posts','search_filter');

You can add parameter :

$args = array(
  'post_status'       => 'publish',
  'posts_per_page'    => 6,
  'paged'             => $paged,
  'meta_key'          => 'event_date',
  'orderby'           => 'meta_value_num',
  'order'             => 'ASC'
);

You can use above parameter e.g. :

function search_filter($query){
    if ( !is_admin() && $query->is_main_query() ) {
        $query->set('post_status', 'publish');
        $query->set('meta_key', 'event_date');
        $query->set('orderby', 'meta_value_num');
        $query->set('order', 'ASC');
    }
}

Related Posts:

  1. Is it possible to sort the post based on a custom field?
  2. How to separate posts in loop?
  3. Loop through custom taxanomy in post and display custom fields from posts
  4. Convert post meta to custom taxonomy?
  5. Count tags for current post and save into custom meta field (and update it on post edit)
  6. ACF – Get unique values of array
  7. Remove Custom metabox from particular page template is used
  8. sort Posts by custom user filed
  9. How can I show custom field according to taxonomy?
  10. How do I create a term for every value of a post meta?
  11. Advanced search form with filters for custom taxonomies and custom fields
  12. How do I exclude a custom taxonomy from the post loop
  13. Is ACF being a honey trap? [closed]
  14. Add custom taxonomy fields when creating a new taxonomy
  15. Display category posts grouped by taxonomy
  16. WordPress database error: [Not unique table/alias: ‘wp_postmeta’]
  17. How to modify the query in taxonomy-custom.php to sort term archives by a custom meta field?
  18. display posts with same taxonomy term
  19. Get value in custom field with taxonomy [closed]
  20. Including all terms in wordpress tax_query
  21. Display one post from each term in a custom taxonomy [closed]
  22. How much faster is a tax query than a meta query?
  23. Remove Category description textarea
  24. Use custom posts as taxonomy term meta replacement?
  25. Custom metabox for menu administration page?
  26. Custom Fields and performance
  27. register_rest_field for custom taxonomy fields that are assosiated with custom post type
  28. Custom taxonomy – custom sortable column
  29. Looping Through Custom Tax Terms and Displaying All Posts For Each
  30. Group posts that matches a term in a loop
  31. Custom taxonomy template loop
  32. How to display submenus on page based on referring top-level parent?
  33. List Custom Taxonomy Values according to a Post Meta Value
  34. How get custom field from custom taxonomy?
  35. Automatically assign taxonomy term if custom meta value exists
  36. How to handle “the_terms” inside loop
  37. Modify main loop in taxonomy archive page
  38. How to add custom field to custom taxonomy in 4.4
  39. how to search in custom fields & custom taxonomy for custom search
  40. Getting yoast title for custom taxonomy
  41. Outputting an array of term meta values in a custom WooCommerce tab?
  42. Storing data into custom taxonomies VS post custom fields (post meta)
  43. get_the_terms() to show all custom taxonomies
  44. List the number of posts for each custom taxonomy and specific custom field value
  45. Single reusable value for post meta: Custom Taxonomy or Post Meta?
  46. Solve product properties with custom taxonomies?
  47. All posts are still shown when adding category argument to query
  48. Is it acceptable to build functionality into a theme built for a client? [closed]
  49. wp_query to find posts by year and month
  50. Create a random unique 6 digit number as custom field for custom post type
  51. Operations with custom fields values in a loop
  52. echo current taxonomy id for this loop?
  53. How do I Use Multiple Loops with WP_Query?
  54. How to adjust code to refresh a dropdown box with tags that are active on filtered category listing
  55. $wpdb query a post type within a specific taxonomy term while ordering posts by custom meta value?
  56. Custom fields to taxonomy
  57. Custom fields ‘for’ custom taxonomy?? Can someone explain why?
  58. How can marge these loop code?
  59. How do I display a list of posts under all hierarchical children of the current taxonomy?
  60. Print terms with taxonomy and metabox value
  61. Meta Key Value in current-user-only loop
  62. How to Filter Posts by Custom Fields?
  63. Styling Taxonomy Terms Individually
  64. Can I use a Custom Meta Value inside of ‘get_the_terms’?
  65. Posts in loop displaying all taxonomies
  66. How to add contents of a custom field to a taxonomy term list?
  67. How can I hide admin columns showing extra custom taxonomy fields?
  68. Conditional display for custom fields/taxonomy
  69. How do I modify the “Insert Media” lightbox in the admin to only show media items from a category?
  70. Limit the number of acf content when displaying in post loop [closed]
  71. Register custom taxonomy from a Advenced custom field option page
  72. Avoiding stripping of HTML in Custom Taxonomy Meta Field
  73. Order by custom field attribute
  74. Trying to achieve row posts below category with multiple loops in custom tax
  75. add custom fields in custom taxonomy meta box in wordpress-3.5.2
  76. Update fields with post object and custom tax with wp_insert_post
  77. Use custom field as tag slug
  78. Custom Taxonomy + JQuery Tabs
  79. WordPress show posts from children terms of a taxonomy term
  80. Advanced search form with filters for custom taxonomies and custom fields
  81. Transfer taxonomy to custom field
  82. Custom Fields link to other pages, not search
  83. Taxonomy Extra Meta [duplicate]
  84. Loop custom post type by taxonomy (Category)
  85. Any way to make custom taxonomy field searchable?
  86. Advanced WordPress search form based on custom taxonomy and custom meta value
  87. Get custom taxonomy value of post and output posts in same taxonomy
  88. More than one Hierarchical Taxonomy and SEO
  89. Order by a meta field in query loop
  90. How much worse is querying custom fields compared to custom taxonomies, quantitatively
  91. How home my code doesn’t display any feature image? Looping through post from a taxonomy and not getting feature images back
  92. Loop posts without any taxonomy
  93. How to looping taxonomy terms?
  94. How to show to show post list under the specific category name using jetengine query?
  95. I want to show image from custom field image on my custom page template
  96. Custom taxonomy with custom meta value is not sorting correctly (query returns the same value for orderby regardless of sort column click)
  97. How to handle custom taxonomy archive when no posts will have that taxonomy
  98. How can I list all the meta in a custom taxonomy?
  99. ACF – How to get custom taxonomy term image field
  100. Grouping posts by 2 different taxonomies
Categories custom-taxonomy Tags custom-field, custom-taxonomy, loop, post-meta
How to display widget in front end?
Using a full-width hero image that can be changed in the dashboard by a user

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