📱

Read on Your E-Reader

Thousands of readers get articles like this delivered straight to their e-reader. Works with Kindle, Boox, and any device that syncs with Google Drive or Dropbox.

Learn More

This is a preview. The full article is published at wpexplorer.com.

How to Include Custom Field Values in WordPress Search

By AJ ClarkeWPExplorer

How to Include Custom Field Values in WordPress Search By default WordPress search works by searching post content and titles. If you have an advanced website where data is store in custom fields, you may want to also search those values. In this guide I will provide you with the code needed to update WordPress so it can search custom fields. All without the need for a 3rd party plugin. If you are not a developer or are scared to add custom code to your website we would recommend using a 3rd party plugin such as SearchWP or Relevanssi . Both of those plugins are completely free but they do offer premium upgrades. Cautionary note : The code provided in this tutorial will make it so WordPress will use ALL custom field values in the search calculation. Depending on your site this could create a security concern or slow down your search queries. In this article Why Search Custom Fields We wrote an article a while back on Why & How to Improve the Internal WordPress Site Search . The article goes over the reasons why you may want to improve the WordPress site search and which plugins are good for the job. So rather then re-iterating everything here, go check out that post. That said, an example may be a website that has a post type for the organization’s staff members. For your staff members you will likely have custom fields to store data such as their job title, skills, education, etc. So, you may want to include these fields in the WordPress search calculation to make it easier to locate members. But before modifying how the WordPress search works, take a second to think if you really need to. There are situations where modifying your search results won’t really provide the best user experience. It may be better to create an AJAX filter so users can select values from various fields to limit the posts by. How to Search by Custom Fields without a Plugin In order to allow custom field values to be included in WordPress search results we will need to hook into three different filters (one optional). We’ll filter the JOIN, WHERE and DISTINCT clauses for the search query. I’ll walk you through each filter and explain what it’s doing. Step 1: Filter the JOIN Clause We’ll start by modifying the JOIN clause via the posts_join filter. /** * Adds the postmeta table to the search query. * * @link https://www.wpexplorer.com/how-to-include-custom-field-values-in-wordpress-search/ */ function wpexplorer_search_posts_join( $join, $query ) { if ( $query->is_search() ) { global $wpdb; $join .= " LEFT JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id"; } return $join; } add_filter( 'posts_join', 'wpexplorer_search_posts_join', 10, 2 ); By default, WordPress is set up to search only the “posts” table and since custom fields are stored inside their own “postsmeta” table we’ll need to include it in the Query. That’s what the previous snippet does. Step: 2 Filter the WHERE Clause Next we’ll filter the WHERE clause by...

Preview: ~500 words

Continue reading at Wpexplorer

Read Full Article

More from WPExplorer

Subscribe to get new articles from this feed on your e-reader.

View feed

This preview is provided for discovery purposes. Read the full article at wpexplorer.com. LibSpace is not affiliated with Wpexplorer.

How to Include Custom Field Values in WordPress Search | Read on Kindle | LibSpace