It’s possible to avoid SQL execution in WP_Query
with the posts_pre_query
filter.
It’s e.g. used by plugins to outsource the default search to 3rd party search engines.
Here’s an example how we can override the main search query on the front-end, with an empty posts array and avoid running the SQL search query:
add_filter( 'posts_pre_query', function( $posts, \WP_Query $query ){
if( $query->is_search() && $query->is_main_query() && ! is_admin() ) {
return array();
}
return $posts;
}, 10, 2 );
Since we’re returning an empty array, I don’t think we need to adjust the
$query->found_posts
or $query->max_num_pages
as they are zero by default.
Related Posts:
- What is “Advanced” SQL?
- Oracle SQL query for Date format
- WPDB Insert or if exists Update
- Count & Display Database Queries
- Where are image paths to product category images stored in WordPress database
- Dealing with Many Meta Values, 30+
- Sharing database for collaborative development
- Hook into $wpdb
- Display data from a non wordpress database on a page template
- Organizing WP Pages based on old website database
- Should I hint table index for search query on wp_post
- Inserting Post Meta From SQL
- Bulk Update of Custom Post Type Post Status through SQL
- How do I add indexes to WordPress Database?
- Moving an entire wordpress site from one domain to a new domain and database
- Change SQL get_results to search for posts with custom term in custom taxonomy
- Hook to be used when creating a database table
- wpdb query on custom table not working [closed]
- How to execute mulitple statement SQL queries using $wpdb->query?
- Delete duplicate rows from wordpress database where a column is duplicate in phpmyadmin
- Query WordPress database by registered date and role
- Using table lock with wp_insert_post?
- Scheduling posts in database
- Size article thumbnails and retrieve them directly from database
- Trigger Plugin database update after git pull
- How to query the WordPress database to get posts of a certain custom post type, taxonomy and field?
- wpdb_prepare with multiple or condition
- Uppercase to sentence case for post titles
- SQL Error wp_commentmeta – incorrect table definition
- How can I create a Bible search engine via WordPress? [closed]
- WordPress and MySQL: how to transfer Meta_key and Meta_Value from one post_id to another
- Hook directly into query execution
- How do I get the posts within a certain year/date(with sql query)?
- Export / import WordPress database
- WordPress is not creating table in database after activating plugin,
- Bulk-create posts from SQL database
- input data and output table [closed]
- Difficulty importing my live site to local
- Replace/Mute/Stop Search Query
- WordPress Database Huge wp_usermeta Table
- wp-cli search is returning ‘search’ is not a registered subcommand of ‘db’
- Create table from array with prepare
- How to Access wp_usermeta Data Immediately After a New User is Created
- How to update the values when the featured image has changed?
- Setup private content for specific users with daily updates [closed]
- Which data is written and stored in the database when read-only (non-posting, non-commenting) users visit the site?
- Insert Query not working in the form
- WPDB SQL query with prepare() returning variable, not db value
- Problems with moving my WordPress site to another domain/server
- Why this query is not showing any result on wordpresss home page?
- Is it possible to have more than 1 MySQL database in my WordPress blog?
- pull data from a remote wordpress database
- How to migrate a database from a server to another
- Clear Terms from Taxonomy for Specific Post IDs?
- Is my way to change WordPress server is correct?
- DB prefix not updating
- Deleting empty meta key values
- Sql query to get all metadata for a post
- I wanted to Know that how can I give ingore to 0000-00-00 in my config file
- Job and Employee Performance Tracking with Product Safety
- adding user_id and name to a separate table whenever a user register based on role
- SQL database import #1046 error
- How do I loop/iterate through posts to edit all img tags?
- Help posting values to DB on submit using $wpdb->query
- What’s the easiest way to connect external database and display data on wordpress?
- Finding a page holding a specific string/URL that is used in the content
- Is there a way for two deferent themes to consume two different DB on a same WP instance?
- How to set posts to draft in bulk based on the content of the post
- How to create index (sql) to a meta_key?
- 2 $wpdb queries causing error Table ‘wp_postmeta’ is specified twice, both as a target for ‘UPDATE’
- Delete all users meta that named: user_avatar
- Correct and secure way to access a custom SQL database in a custom PHP template file
- About wp database hooks (error establishing connection)
- How to get specific table by current user login
- The MySQL Connection could not be established., before it was joomla but i have change it into worpress
- How to retrieve user data based on role using SQL?
- $wpdb not working
- WordPress character set issue
- Filtering data and saving query results
- Link wordpress with a SQl + MongoDB + .NET
- How to have WP Search widget index dynamically generated content?
- How to add data from wordpress tables to new database tables you have created
- Searchable database of members using multiple criteria / filters
- Getting deleted users in database
- Install WordPress with SQL database
- Migrated to Namecheap, now Trouble with Database & wp-config [closed]
- export individual posts to text files or a single csv file
- SQL query : Select a thumbnail for a media
- Attach posts based on category and position
- database search feature
- why you don’t Implement PDO for access every thing to database
- SQL Query to get post_id from wp_posts and and meta_key(s) from wp_postmeta
- SQL query to rewrite all media URLs to end with .webp
- How do I find users by password?
- Weird WP -Cli Error Connection Refused
- Order of search results based off of a backend custom field and also in alpha order
- How to disable publish_future_post auto add wp_options cron
- #1067 – Invalid default value for ‘post_date’ when trying to create a new column
- Deleting WordPress posts and media files based on author via MYSQL database
- Database table prefix different between wp-config.php and in database