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

Searching by meta values showing inappropriate result

First of all, one never should rely on raw $_REQUEST: believe me, this is a security issue.

PHP has 2 functions: filter_input and filter_input_array that helps you to sanitize your request data.

In addition, in your request dump I don’t see any $_REQUEST['custom_bath'], but you are using that value, that can be a cause for the issue. You should check that a variable is setted before use it.

Using my suggestions your code becomes something like this

$args = array(
  'custom_textarea'   => FILTER_SANITIZE_STRING,
  'custom_price'      => FILTER_VALIDATE_INT,
  'custom_price1'     => FILTER_VALIDATE_INT,
  'custom_beds'       => FILTER_SANITIZE_STRING,
  'custom_bath'       => FILTER_SANITIZE_STRING,
  'custom_garage'     => FILTER_SANITIZE_STRING
);

$get = filter_input_array( INPUT_GET, $args, TRUE );
$post = filter_input_array( INPUT_POST, $args, TRUE );
$request = array_merge( $get, $post );

$query_args = array( 'relation' => 'OR' );

if ( ! empty( $request['custom_textarea'] ) ) {
   $query_args[] = array(
     'key' => 'custom_textarea',
     'value' => "%{$request['custom_textarea']}%", 
     'compare' => 'LIKE'
   );
}

if ( ! empty( $request['custom_price'] ) && ! empty($request['custom_price1']) ) {
   $query_args[] = array(
     'key' => 'custom_price',
     'value' => array( "{$request['custom_price']}", "{$request['custom_price1']}" ), 
     'compare' => 'BETWEEN',
     'type'    => 'SIGNED'
   );
}

if ( ! empty( $request['custom_beds'] ) ) {
   $query_args[] = array(
     'key' => 'custom_beds',
     'value' => "{$request['custom_beds']}", 
   );
}

if ( ! empty( $request['custom_bath'] ) ) {
   $query_args[] = array(
     'key' => 'custom_bath',
     'value' => "{$request['custom_bath']}", 
   );
}   

if ( ! empty( $request['custom_garage'] ) ) {
   $query_args[] = array(
     'key' => 'custom_garage',
     'value' => "{$request['custom_garage']}", 
   );
} 

$query = new WP_Query( $query_args );  

Related Posts:

  1. How to make search include data from wp_postmeta?
  2. WP User Query with search columns and meta query
  3. Combining Meta_Query key values for one array
  4. pre_get_posts and search query for admin
  5. Comparing between a negative and positive number
  6. Advanced Search by minimum/maximum values
  7. WordPress, fetching users with an exact match in a string of comma separated values in user_meta
  8. How to make search include data from wp_postmeta?
  9. Meta query: How do I return posts within a date period from a given month?
  10. How do I have WP_Query match posts based on search parameter OR meta fields? (rather than search parameters AND meta fields)?
  11. meta_query issue with multiple numerics
  12. Modifying post content with the_post action hook
  13. Custom Query Content Filtering
  14. How to query two meta fields and display results between them
  15. Search by meta_query
  16. Exclude posts with certain meta data from search results
  17. meta post search
  18. Form for search pages by meta datas
  19. search for single meta_key with numeric array of meta values
  20. WP search in metadata post
  21. meta field search too restrictive in pre_get_post() for custom search
  22. Consistent and Admissible Heuristics
  23. Solr vs. ElasticSearch
  24. How do you make Vim unhighlight what you searched for?
  25. grep for special characters in Unix
  26. Search is always exact [closed]
  27. Using meta query (‘meta_query’) with a search query (‘s’)
  28. Advanced search form with filters for custom taxonomies and custom fields
  29. how to limit search to post titles?
  30. How to create live autofill search?
  31. How can I implement a location based (zip code) search in WordPress?
  32. How to highlight search terms without plugin
  33. How do I remove Pages from search?
  34. Empty search returns home page, how to return not found search page?
  35. How to search for (partial match) display names of WordPress users?
  36. How does WordPress search work behind the scenes?
  37. Use REGEXP in WP_Query meta_query key
  38. Remove some pages from search
  39. Limit search to latin characters
  40. How to look at code in WordPress repositories without downloading?
  41. Extend WordPress search to include user search
  42. Display search results within the same page
  43. Empty search input returns all posts
  44. How can i move search results onto a specific page?
  45. Remove meta robots tag from wp_head
  46. WordPress blog with 30 000 posts: poor search performance
  47. Template issues getting ajax search results
  48. WordPress database error: [Not unique table/alias: ‘wp_postmeta’]
  49. Insert DIV container below 1st search result
  50. Unified search across separate WordPress installations
  51. Search in WordPress – Difference of searchpage.php, searchform.php and search.php?
  52. How to make WordPress search prioritise page titles?
  53. How to add the author search in the default wordpress search?
  54. Add custom fields to search
  55. Admin Area Custom Type Search By Meta Fields Without Title & Content
  56. How do I filter the search results order?
  57. How to insert a span inside a search form?
  58. How do I redirect /search/ to wordpress search template?
  59. How do I Paginate Search Results for Custom Post Types?
  60. Dealing with Many Meta Values, 30+
  61. How to search for users based on added user metadata
  62. Search by Post ID and display content of the post in search result
  63. Extending search query with additional $sentence value
  64. Link to search page without search parameter
  65. Search/Replace for editor in HTML-mode
  66. How do I search events between two set dates inside WP?
  67. Search only blog posts (default WP search widget)
  68. Meta compare with date (stored as string) not working
  69. Getting attachments by meta value
  70. Exclude pages from WordPress search result page
  71. How to pass a search $_GET parameter to a new custom search page?
  72. Do I need to sanitize WordPress search query?
  73. Disable Redirect to Product Page on Search Results Page in WooCommerce [closed]
  74. Search Using Post ID
  75. Ordering posts having multiple post-meta date fields
  76. Search custom post type by meta data
  77. Neither the_excerpt() nor the_content() works properly?
  78. Let user change posts per page
  79. Custom Queries: Joining On Meta Values From Two Custom Post Types
  80. Exclude top-level pages from search results
  81. WordPress 3.6, searchform.php problems
  82. Search in non-English language returns only one result
  83. Complex Search functionality. Advice needed
  84. Get user role by using user_id in buddypress
  85. How to use next_post_link and previous_post_link on single posts in search results
  86. Why is there a class=”screen-reader-text” on my search button?
  87. template_include for search.php makes WordPress think its on the home page
  88. Turning Broken URLs Into Search Terms?
  89. How Can I save multiple records in same meta key?
  90. How can I implement faceted search with WordPress 3.x?
  91. can’t limit search to only pages
  92. Meta query interfering with orderby relevance
  93. WooCommerce conditional meta query
  94. Add custom parameters to JSON API search query?
  95. Change the Search Base in a multi language wordpress
  96. Display posts from an author using search parameters (not author template/query_posts)
  97. Search everything (posts, pages, tags, cpt, meta)
  98. Search: Only One Result Returned
  99. How to update single value in multi dimensional Post Meta?
  100. What is an efficient way to query based on post_meta?
Categories search Tags meta-query, post-meta, search
Minimize database queries to user tables?
basic shortcode – Why 1st paragraph not wrapped in p tag, but 2nd is

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