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
- 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?
- 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
- Export / import WordPress database
- WordPress is not creating table in database after activating plugin,
- Bulk-create posts from SQL database
- 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?
- 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?
- Why does changing Database Table Prefixes result in loss of content?
- Options on how to Display member specific data from a MS SQL database
- Delete/Replace words from X to Y in post content
- How to delete a particular row in a database table
- Delete all posts that do NOT have a specific tag
- Location of WP default mySQL tables creation file?
- Custom search query by category id
- SELECT SQL_CALC_FOUND_ROWS with wordpress search
- $wpdb->get_var – What value is returned when zero records are found?
- Understanding WordPress Search
- How to display data from db in select list [closed]
- WordPress SQL – How to Check for Category?
- Getting value from database table depending on field value
- custom tables in wordpress Database? (can i just create them with sql?)
- Search Character Set Problem
- Accidently changed the GUID
- 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
- 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
- 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)
- 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
- 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