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. How to display SQL query that ran in query?
  51. Advanced search form with filters for custom taxonomies and custom fields
  52. How can I create a meta_query with an array as meta_field?
  53. Query multiple meta key values?
  54. How to Optimize WP site for millions of posts
  55. order by numeric value for meta value
  56. How to make search include data from wp_postmeta?
  57. How to get comments by post ID?
  58. Differences between wpdb->get_results() and wpdb->query()
  59. Is there a way of increasing the speed of this query?
  60. Get all image from single page using this query
  61. How can I query all users who registered today?
  62. Get Terms by IDs with IDs order
  63. WordPress database error: [Not unique table/alias: ‘wp_postmeta’]
  64. Reversing the order of posts AFTER the query is performed
  65. simple sql query on wp_postmeta very slow
  66. Ascending & descending posts on same page with IF statement
  67. How do I find users by password?
  68. Search query alteration not working for meta values
  69. Prevent executing query for wp_global_styles
  70. Search results stuck on page 1
  71. How to use a dynamic term id for the query block
  72. Querying Posts from ACF Repeater Subfield Select Value
  73. How to figure out correct wp_options to have autoload=’yes’
  74. Advanced Search – Is this possible?
  75. Meta Query compare with LIKE pulls similar post types: 55 and 155, and 1,155
  76. How to show sticky posts on all pages of the pagination, not just the first page?
  77. Extend search query to search meta keys values based on search string
  78. Searching for a custom meta from user.php in the admin
  79. Search for meta_query does not return any result if combined with title
  80. How to get posts that have certain meta key value and order based on another meta key’s value
  81. Why does $_GET return values but get_query_var does not?
  82. Sort users in admin screen by meta value whether it exists or not
  83. is it possible to get a list of URLs from post_content directly in a mysql query via phpadmin?
  84. Modify WordPress Search
  85. Can I make a Heading block above a query loop block disappear when that query loop has no results?
  86. Get URL parameters with rewrite rules
  87. Elementor – Sort by name
  88. Rewrite URL custom search query
  89. pre get posts not working with Tribe Events
  90. How do you create a query to hide all posts with a certain meta key, unless current user is the author of that post, without custom SQL?
  91. Search posts by meta key in admin and front
  92. Custom API how to return int field
  93. import/view table from external site
  94. Problem in MySql query on WordPress [closed]
  95. Can I filter existing search query using pre_get_posts for post_mime_type?
  96. 404 with google ads query params
  97. Add specific postmeta to search query
  98. “Page Not Found” with multiple last pages of query
  99. prepare select of array
  100. How to allow searching a custom meta key in admin list table?
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
  • 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