wp_query search multiple keywords

wp_query search multiple keywords

Rate this article

Share this article

Read Add a WordPress Search Form in 5 minutes – No code keywords for more information.

WP_Query

WP_Query deals with the posts in an intricate manner as defined in the wp-includes/class-wp-query.php. If you have a whole number of multiple queries, then you can perform multiple loops.

wp_query search multiple keywords

 

 WordPress also provides to its users the facility to carry on more filters to change the WP_Query with the use of those filters. Also, you must know that the WP_Query accepts only the string of single keywords joined with the help of the + character.

You can also try to write a query where you will be needed to do a full-text search against multiple keywords. At any time, you will get a set of words and it will be good for you if you can query on multiple bits of the metadata.

Moreover, with the WP_Query search for multiple keywords, you will put multiple fields in the query parts. This can be done by adding the extra remaining nesting levels in the URL string.

Firstly, I think you should use WP_Query instead of query_posts. On their documentation for query_posts it mentions:

Note: This function isn’t meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query.

Assuming you’d switch to WP_Query, the search parameter or ‘s’ value doesn’t seem to accept an array. According to the documentation, you can only use a string.

Adding an array to this will cause WP_Query to ignore the array.

An approach I followed on a project recently was:

  • See the actual SQL query made by WP_Query You can do this by
// Your sample query
$query = new WP_Query( array( 'cat' => 4 ) );
// Print the SQL query
echo $query->request;
  • Create a custom SQL query based on the one above Have a look at the examples shown in the documentation for “Displaying Posts Using a Custom Select Query”.

It should look like

global $wpdb;
global $post;
$querystr = "
    SELECT DISTINCT wposts.* 
    FROM $wpdb->posts wposts
        LEFT JOIN $wpdb->postmeta wpostmeta ON wposts.ID = wpostmeta.post_id 
        LEFT JOIN $wpdb->term_relationships ON (wposts.ID = $wpdb->term_relationships.object_id)
        LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE wpostmeta.meta_key = 'customDateField'
        AND wpostmeta.meta_value >= CURDATE()
        AND $wpdb->term_taxonomy.taxonomy = 'category'
        AND $wpdb->term_taxonomy.term_id IN(1,2)
    ORDER BY wpostmeta.meta_value ASC
LIMIT 4
";
$wpdb->get_results($querystr, OBJECT);

You should note that data passed to a custom query, need to be sanitized manually.

                     

The WP_Query Generator is a helpful tool for the developers. By using this tool, custom code for the WordPress query with WP_Query class can be created very easily.

Click here to know more:

YouTube video

Wordpress search engine to search multiple keywords
Wordpress offers lots of custom search options. One can decide how the search should behave and how users interact with it. We have various custom search plugin for Wordpress. If you have not already implemented the search then you can refer to how to implement custom search in WordPress to get more context. It’s also sometimes called a custom search module. Most plugins come with their own default search engine results page (SERP). But you can set a custom search page or custom search results page from the plugin settings. Once a custom search results page is ready one might also need a WordPress search in custom fields to have the flexibility of adding custom search fields. Metafields are extra pieces of data that apps can attach to products, customers, orders, and other objects in the eCommerce universe. They are useful for storing information that doesn’t otherwise have a home in the admin section. You can search meta fields via a search plugin. Along with searching in meta tags, you can have the WordPress search with filters. Check this out if you face any with WordPress search form not showing results.
Are you showing the right products, to the right shoppers, at the right time? Contact us to know more.
You may also like