Search in WordPress is complicated matter because:
- WP doesn’t do it very well out of the box
- People tend to have varied expectations of how precisely search should work
- Even light (from human perspective) requirements for search tend to translate in complicated and involved code
Building search from scratch is involved (I know because I did exactly that for a while for clients) and you should totally look for search plugins or other compromise (like search as service) first.
To briefly enumerate specific issues with your code:
- custom Ajax endpoints generally should not be used, see AJAX in Plugins
- using raw MySQL function to query almost never used, see wpdb (and especially parts about securing your queries against injections)
query_posts()
should not be used, see When should you use WP_Query vs query_posts() vs get_posts()?
Related Posts:
- Retrieve and display data from custom db table in admin area?
- How to find if a post with custom_field == X exists?
- Hi, I wanted to make custom search with multiple fields
- $wpdb->replace: WHERE?
- Admin search post meta
- Custom Loop Query For Search Page
- Applying WP-cli Search & Replace to Static SQL Dump File
- Custom Same Page Search
- How to Add Additional Search Button?
- WP Search Form Query: Add Author’s posts into search query
- Adding overlay search to wordpress using add action/filter
- Search a meta field for a value in all posts
- WordPress Paginate $wpdb->get_results
- Modify existing plugin function using filter (without modifying the plugin directly)
- Backup the Database and Restore from the Backup?
- Limit Media Library to Given Folder
- Filter the query ONLY for the search results page
- What is the proper way to apply the login_form_bottom filter?
- Creating search form for custom database table
- How-to inspect filter-able $vars?
- How to limit search to first letter of title?
- Can I use $wpdb for my custom tables in the WordPress database?
- Create subpage /user/ or /my-profile/ like /author/ with additional query like /user/user123
- Customise search form in the Widget
- Comment filtering (search)
- What Is The Difference Between suppress_errors() And hide_errors() in $wpdb?
- Combine multiple words in custom meta search query
- Custom Queries – Child Categories not showing up
- Suggestions for WPMU Migration (advanced)
- Customizing the default logout page of WordPress
- What’s the proper way to use a custom table? [closed]
- How to retrieve the value stored in a multi-dimensional array and SUM the entries?
- Adding a Table to the wordpress database
- Creating user relationships
- WP Northwind for Theme Development?
- Add options to the “Screen Options” section on the “Menus” editor
- How I display post of the post type group by year of post?
- Custom URL going to 404
- Search result page redirect ex.com/?s=searchterms to ex.com/searchterms
- Load more posts with multiple queries
- Fastest way to get the comment and ping total count for a post
- woocommerce_checkout_fields modifying existing billing_phone label created new field
- changing font size and font of a custom message added after comment
- result from custom filter admin table list backend does not work
- Custom search to display results within same page
- Search widget will search everything in the site, how to limit to only search gallery name
- wp_query get post custom taxonomy in search.php
- Order posts with a custom field but also without
- How to hook up search results page to a back-end page?
- Save title as custom field on custom post type
- Combine two conditions in one statement syntax [closed]
- Custom Search on Custom Page while preserving the main search function
- How to create custom search page
- Setup database structure with books, authors, languages etc. for publishing house [closed]
- Dynamically Filtering Posts by Data Points – Any Pointers?
- Add table to WordPress database
- Display posts by combining multiple meta key
- How to append custom text to the output of ‘categories widget’?
- Custom select query two tables by a meta key
- How to query term_id of a Custom Taxonomy by name
- Custom Database Table and foreach
- How Can I Always Display A Particular Post First Using WP_Query?
- query variables for custom taxonomies
- optimal taxonomy or custom post types
- display text generated via custom get query string
- New Page/Post Screen Opens an Existing Post
- How to extend WP_query to a specific table?
- WP_User_Query Search Multiple Keywords from a Multi-Select Field?
- Custom query pagination with php parameter as arg problem
- How to have differents sites share the same tables of DB?
- Displaying posts on a page that have been assigned a value in a database
- Add to search posts query array with post IDS which will appear first
- Query Posts to display all posts with category “custom field value”
- Search Replace Database ONLY for posts of certain category?
- How to append the excerpt to the content in the single post page?
- Custom dropdown search form that allows users to select option B based from Option 1
- Accountless password generation
- Where should I put this custom data?
- Creating a search with tag variable also added for more detailed results
- Show WP content on different PHP Sites
- Multiple Taxonomies Using Custom SQL Query
- Changing starting number of User ID
- Apply `the_content’ filter to theme customisation live preview
- Two searchforms with different categories/posts per page
- Custom WordPress SQL Query 4 posts per custom taxonomy
- Search results load individual post in slide out div
- Accessing + retrieving custom database in WordPress
- Custom MySQL query to list post
- Custom Query for Taxonomy
- Create a custom search for Estate Agency WordPress site
- database search feature
- Include plugin´s table in custom query
- Add variable : 1 Week ago
- Advice how to connect custom database with WordPress, change data in WordPress and finaly use this data
- Sort Search Results in Order of/by Character @ count (9) of post(s) title. (Ocean-WP theme)
- Show total sales for each products on search results page Woocommerce
- Changes to code not displaying
- User Query Multiple Orderby Clause
- Can I log the searches that are returning 404 in the DB?
- Custom Search on media files PDF images pages posts