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

filtering custom post types via meta data drop down

Because you want the results of the filtering process be the main object of your page, you may need to alter the main query using pre_get_posts action hook. It is really better that leave the main query run, load your theme and then say to WordPress: go back and run another query as suggested in other answers.

//Functions for filters
add_action( 'pre_get_posts', 'my_pre_get_post' );
function my_pre_get_post($query){

     //limit to main query, frontend and archive pages. You need another set of checkings, like check if the request is for your custom post type
     if($query->is_main_query() && !is_admin() && $query->is_archive ) {

          $meta_query = array();
          $meta_input = isset($_POST['meta_input']) ? $_POST['meta_input'] : '';

          if(!empty($meta_input)){
              $meta_query[] = array(
                  'key'  => 'your_meta_key',
                  //sanitize $_POST['meta_input'] according with your data type
                  'value'    => $_POST['meta_input'],
              );
          } 
          $query->set('meta_query',$meta_query);
    }

}

See more information about the meta_query.

Related Posts:

  1. Filter results with custom field values and dropdown
  2. WordPress Search Custom Meta Field Only
  3. How do I use wp_query for WordPress search?
  4. How do I have WP_Query match posts based on search parameter OR meta fields? (rather than search parameters AND meta fields)?
  5. Custom WP_Query for WordPress Search Results with meta_query
  6. How can you include custom post meta in search without calling each key?
  7. How to display childrens custom fields?
  8. Footnotes in custom fields
  9. searching by keywords in post’s metas or pagination links problem
  10. Search one custom field?
  11. Query postmeta values, and return multiple post_titles for common meta value
  12. change attachment custom field onChange event
  13. Search for meta_query does not return any result if combined with title
  14. Add filter menu to admin list of posts (of custom type) to filter posts by custom field values
  15. Using meta query (‘meta_query’) with a search query (‘s’)
  16. Can I exclude a post by meta key using pre_get_posts function?
  17. Advanced search form with filters for custom taxonomies and custom fields
  18. Custom post meta field effect on the performance on the post
  19. How to get custom post meta using REST API
  20. Difference between meta keys with _ and without _ [duplicate]
  21. Is there any action filter/hook for validating a custom field before publishing the post?
  22. Orderby meta_value only returns posts that have existing meta_key
  23. What is the index [0] for on post meta fields?
  24. What is “meta_input” parameter in wp_insert_post() used for?
  25. How to enable revisions for post meta data?
  26. The “_encloseme” Meta-Key Conundrum
  27. Best way to programmatically remove a category/term from a post
  28. Filter WP_Query for posts having a certain meta-value
  29. Using get_post_meta with new_to_publish
  30. Including custom fields in search?
  31. Add custom fields to search
  32. Admin Area Custom Type Search By Meta Fields Without Title & Content
  33. Apply the_content filter to a custom field with multiple values
  34. Custom field metabox not showing in back-end
  35. How do I Paginate Search Results for Custom Post Types?
  36. So much data in postmeta
  37. Custom search: by post data and post metadata?
  38. Can I count the number of users matching a value in a multiple value key?
  39. Pass all custom fields through the same filter on post load?
  40. Need small coding with Custom Fields Search
  41. When using add_post_meta and update_post_meta, is there any way to give the individual arrays keys?
  42. How to hide meta box values from custom fields list?
  43. Auto sort the wp-admin post list by a meta key
  44. get_post_meta() unserialize issue – returns boolean(false)
  45. What is the advantage of the wp_options design pattern?
  46. Search ONLY by meta key / meta values
  47. Storing meta fields multiple times OR once with multi dimensional array?
  48. Allow user to create instances of custom field
  49. display specific custom fields
  50. Is there a hook / action that is triggered when adding or removing a post thumbnail?
  51. Meta keywords and descriptions plugin for manually editing meta for each page/post
  52. Add custom field (value) to search result (without plugin)
  53. passing argument to get_template_part() or a better way to code
  54. Is it possible to store arrays in a custom field?
  55. Archive sorting functions by custom fields (front-end)
  56. Get updated meta data after save_post hook
  57. Multiple meta values for same meta_key adding on “Preview Changes” hit but not on saving or updating post
  58. Save HTML formatted data to post meta using add_post_meta()
  59. Search everything (posts, pages, tags, cpt, meta)
  60. importing data from non-wordpress mysql db
  61. Is there a way to do multiple ordering on a multiple meta_query?
  62. searching in custom meta field
  63. Create meta boxes that don’t show in custom fields
  64. Transients vs CRON +Custom Fields: Caching Data Per Post
  65. Unable to save datetime custom meta field using update_post_meta() function
  66. Up/Down voting system for WordPress
  67. Change content before writing to database
  68. post meta data clearing on autosave
  69. Create custom field on post draft or publish?
  70. Display info from custom fields in all images’ HTML
  71. Ordering posts by anniversary using only day and month
  72. How do I assign this filter to a variable? (Appending php & markup to the_content)
  73. Exclude custom post type from search by custom field value?
  74. get_post_meta fields don’t show up on posts page
  75. Using custom fields in a filter hook
  76. Update meta values with AJAX
  77. How to break meta values into different items and avoid duplicates?
  78. copy attachments to another post type and change attachment url
  79. Cannot edit post meta fields with rest API
  80. ajax delete value from custom field array
  81. Save attachment custom fields on front end
  82. Add an advert every nth Paragraph
  83. How to use pagination with get_post_meta
  84. Copying Custom Meta Values from existing post to a duplicate post
  85. Add a post meta key and value only if it does not exist on the post
  86. Move value of one custom field to another
  87. Order posts according to user defined order for meta values?
  88. Displaying posts with only upcoming dates according their custom field date value
  89. How to filter a dd/mm/yyyy date from a custom field in a query
  90. Custom fields to save multiple values
  91. Custom fields: In what order are they saved into the DB?
  92. Function to change meta value in database for each post
  93. Get a post_id where meta_value equals something in a serialized meta_value field
  94. Get aggregate list of all custom fields for entire blog
  95. Transition from (classical) serialized custom meta field to (gutenberg) rest enabled meta
  96. Unable to show ACF’s Image Custom Field properly in Genesis Framework [closed]
  97. Custom field value based on other custom field values
  98. wp_handle_upload error “Specified file failed upload test” but still creates attachment?
  99. How to save a ToggleControl value in a meta field?
  100. Extend ‘The Events Calendar’ search to include custom fields [closed]
Categories custom-field Tags custom-field, filters, post-meta, search
Allow users of my plugin to define their own shortcode rather than use mine?
How do you return a menu with the menu name?

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