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

Sort by postmeta on when searching

Problem was that I had a plugin installed that deals with the searches, it’s called Better Search.

function query_1($query){
    $query .= ",wp_postmeta.meta_value as total_saless ";
    return $query;
}
add_filter( 'bsearch_posts_match_field', 'query_1' );

function query_2($query){
    $query .= "AND wp_postmeta.meta_key='total_sales' ";
    return $query;
}
add_filter( 'bsearch_posts_where', 'query_2' );

function query_3($query){
    $query = " CAST(total_saless AS INTEGER) > 99999 DESC, score DESC ";
    return $query;
}
add_filter( 'bsearch_posts_orderby', 'query_3' );

All I had to do was to hook into it.

Related Posts:

  1. Search custom post type by meta data
  2. what are the numbers between curly brackets in search query
  3. Search Terms – Querying on either description__like OR name__like in the same Term Query?
  4. How to display liked posts of current user in wordpress?
  5. Filter query posts by the count of metas for a meta key
  6. search through post-type attachments titles
  7. Save default value for empty or missing post meta
  8. Grouping related postmeta data via SQL query
  9. mySQL query. ORDER BY meta_key
  10. Remove posts from query for events whose start date has passed
  11. Change searched term
  12. Query metas (and not : query posts by metas)
  13. Search query -> Show all pages except certain template
  14. Temporarily storing main search result
  15. Query based on title, with ‘compare’ => ‘IN’
  16. is_search called incorreclty
  17. Issue with using add_rewrite_rule() for post querying custom fields
  18. Attempt to improve WP search, can someone check my SQL query?
  19. Searching With Apostrophe
  20. Query posts according to specific post meta values
  21. How to reset usual $query on search page to push custom $wpdb query there?
  22. Order by meta value pro first then meta value free in my search function
  23. How to query two meta fields and display results between them
  24. Ideas how to search & replace post_content when string contains a newline?
  25. How to insert Collate into WordPress search query?
  26. Slow Search Queries – How to exclude pages, media, excerpt, authors, private posts?
  27. How to interfere to default search to make it search in custom fields?
  28. How to manage a particular “order by” for get_search_query()?
  29. get_post_meta with WP_query
  30. Declaring a var, placing it in a query and using the output of the query?
  31. Combining two meta_values within one row with query
  32. WordPress search in modified post title
  33. Filter orders by modify date
  34. How to add url parameter to every search query in SearchWp?
  35. Add rewrite rule for archive search
  36. Restrict WordPress search to a single ACF field
  37. Is there any way to get all custom posts and all custom terms with it’s meta in few queries?
  38. Modify behaviour of “s” parameter in wp_query
  39. searching by keywords in post’s metas or pagination links problem
  40. Pagination adds search query (again)
  41. Speed up search query that searches in post meta?
  42. How to make MySQL search queries with quotes
  43. Use $wpdb->get_results with filter based on array
  44. $query->set( ‘post_type’, ‘post’ ); not working
  45. What is the best way to query posts based on live data?
  46. Custom MySQL Query for Post and Post Meta
  47. search.php is not generating the right results
  48. Run search query again without pagination gives no results?
  49. is_search called incorrectly
  50. simple sql query on wp_postmeta very slow
  51. wordpress query – orderby child post date
  52. How do I search events between two set dates inside WP?
  53. Compare two numeric custom fields
  54. Custom query filter not working on woocommerce category page
  55. How to delete a transient on post/page publish?
  56. Fulltext search in posts sorted by relevancy
  57. How to create a WP_Query to search the Title or Tag?
  58. WordPress Search Filter Only for Page with Child of Child of Child of Child of Child
  59. WordPress creating excessive joins on meta_query with search
  60. If No Search Results, Show Posts from Alternate Search Query
  61. Search query – exact post title match
  62. How to remove duplicate query on page load or make them cacheable
  63. Pull post meta with post_query?
  64. Huge amount of queries on my site
  65. WordPress Orderby Numeric Value Not Working
  66. Searching by meta values showing inappropriate result
  67. How do I use get_query_var() within plguin code
  68. How to display related posts by same publish date?
  69. Get post according to current taxonomy
  70. filtering custom post types via meta data drop down
  71. Alter query with posts_clauses to retrieve NULL values last
  72. WordPress Query orderby title with UTF8 string in title
  73. get_posts showing only 5 posts. (nopaging and posts_per_page -1 not working)
  74. Display posts between two particular dates
  75. Order posts by date, but also give priority for a specific term
  76. $post->ID not working in combination with a custom query
  77. Combine “Exclude” Query in functions.php
  78. Wrap/close ‘query’ for a first-letter ordered listing of posts
  79. wpdb query with dynamic column name?
  80. How do you display the number of a post in a query?
  81. How can I get the name of term post meta value which equals term id
  82. WP_Query meta compare must include ALL array values
  83. Searching with Multiple (Parallel) Criteria on Custom Fields?
  84. get products by custom date variable
  85. Compare User meta_value with Product meta_value
  86. How to delete all images from code in all post_content
  87. How to sort by two meta keys in admin area?
  88. wp query multiple values > display a specific value first
  89. PHP Fatal error: Uncaught Error: Call to a member function insert() on null
  90. Nested select statements not working
  91. Add to search posts query array with post IDS which will appear first
  92. OR condition not working
  93. Add Hook for clearing transient when post is added
  94. Show links to child pages on both parent AND child pages
  95. Modify Global Posts Plugin
  96. SQL: Search query to get attachments only of those parents which are published
  97. How to reference same column name but different table in custom db query
  98. Retrieving custom field array value through db query
  99. SQL Query : how copy all tags of post into their post content in wordpress by sql query
  100. AJAX loading with custom parameters
Categories query Tags post-meta, query, search
WordPress load javascript file if something… (after the page is loaded)
Generate new URL for popup form

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