Using wordpress default search works by using a look at the submitted content, subject, and titles. When you have a complicated web page where data is stored in custom fields, you might want to search for those values as well. In this data I will be able to provide the code needed to modify wordpress to search for custom fields. All without the need for a third-party plugin.
Even if you are not a developer or are afraid of uploading custom code to your website, we recommend using a third-party plugin related to SearchWP or Relevanssi. Both vital plugins are completely free, but still offer the most reasonable fee upgrades.
Be careful to realize: The code provided in this statement will cause wordpress to use ALL custom subject values when calculating the search. Depending on your website, this could create a security issue or slow down your search queries.
Why Look for Custom Fields
We wrote an editorial a while back on Why and How You Can Strengthen Your wordpress website Internal Search. The article looks at why you would want to strengthen your wordpress website search and what plugins are very good for the method. So, reiterating the whole thing here, go check out that article.
That said, an example would be a web page that has a submission type for crowd staff members. In the staff members you will almost certainly have custom fields to store data related to their identification procedure, skills, coaching, and so on. So, you would want to include those fields in the wordpress search calculation to allow you to find the members.
Alternatively, before you improve the way your wordpress search works, take a second to think about when you actually want to do that. There are times when improving your search results won’t actually provide the perfect individual experience. It might be better to create an AJAX filter so that shoppers can select values from various fields to narrow down posts using.
How to Search Using Custom Fields Without a Plugin
To allow custom subject values to be incorporated into wordpress search results, we may want to hook into 3 different filters (one not needed now). We will filter the JOIN, WHERE, and DISTINCT clauses for the search query. I will walk you through each individual filter and provide an explanation of what it does.
Step 1: Delete the JOIN clause
We will start by improving the JOIN clause by using posts_join
filter.
/**
* Supplies the postmeta table to the hunt 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() ) {
world $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 );
With the default usage, wordpress is able to look up the “posts” table more efficiently, and since custom fields are stored in their own “postsmeta” table, we want to include them in the query. This is what the above snippet does.
Step 2: Remove the WHERE clause
Next we will filter the WHERE clause using the hook in posts_where
hook.
/**
* Supplies meta values to the hunt query.
*
* @link https://www.wpexplorer.com/how-to-include-custom-field-values-in-wordpress-search/
*/
function wpexplorer_search_posts_where( $where, $query ) {
if ( $query->is_search() ) {
world $wpdb;
$where = preg_replace(
"/(s*{$wpdb->posts}.post_titles+LIKEs*('[^']+')s*)/",
"({$wpdb->posts}.post_title LIKE $1) OR ({$wpdb->postmeta}.meta_value LIKE $1)",
$where
);
}
return $where;
}
add_filter( 'posts_where', 'wpexplorer_search_posts_where', 10, 2 );
The above code tells the wordpress search query to search all meta_value columns. Again, this will most likely include all custom fields. While you only want wordpress to see specific fields, the code is usually much more complex.
Step 3: Remove the DISTINC clause (now no longer needed)
Finally we will filter the posts_distinct
hook.
/**
* Prevent reproduction posts in search results.
*
* @link https://www.wpexplorer.com/how-to-include-custom-field-values-in-wordpress-search/
*/
function wpexplorer_search_posts_distinct( $where, $query ) {
if ( $query->is_search() ) {
return "DISTINCT";
}
return $where;
}
add_filter( 'posts_distinct', 'wpexplorer_search_posts_distinct', 10, 2 );
This last little bit of code prevents the rendering of search results while you have been provided with custom fields with equivalent values added to the equivalent submission in numerous fields. This is not a problem in most cases, but it is worth mentioning in case. It actually does no harm to load the code anyway (at least I don’t think so).
PHP Class and Plugin
To make it easier, I have compiled all 3 snippets above into one neat little thing that you will add to your website. Using a class will keep the code separate and organized. You will add the code in your child theme’s functions.php or in a code snippet plugin.
I like to suggest along with elegance PHP in its own private report in your child theme and load it using require. This can occasionally keep your code well organized as a substitute for having a large functions.php.
/**
* Allow taking a look out by the use of custom fields.
*
* @link https://www.wpexplorer.com/how-to-include-custom-field-values-in-wordpress-search/
*/
final elegance Search_By_Custom_Fields {
/**
* Class constructor.
*/
public function __construct() {
add_filter( 'posts_join', [ $this, 'filter_posts_join' ], 10, 2 );
add_filter( 'posts_where', [ $this, 'filter_posts_where' ], 10, 2 );
add_filter( 'posts_distinct', [ $this, 'filter_posts_distinct' ], 10, 2 );
}
/**
* Supplies the postmeta table to the hunt query.
*/
public function filter_posts_join( $join, $query ) {
if ( $query->is_search() ) {
world $wpdb;
$join .= " LEFT JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id";
}
return $join;
}
/**
* Supplies meta values to the hunt query.
*/
public function filter_posts_where( $where, $query ) {
if ( $query->is_search() ) {
world $wpdb;
$where = preg_replace(
"/(s*{$wpdb->posts}.post_titles+LIKEs*('[^']+')s*)/",
"({$wpdb->posts}.post_title LIKE $1) OR ({$wpdb->postmeta}.meta_value LIKE $1)",
$where
);
}
return $where;
}
/**
* Prevent reproduction posts in search results.
*/
public function filter_posts_distinct( $where, $query ) {
if ( $query->is_search() ) {
return "DISTINCT";
}
return $where;
}
}
new Search_By_Custom_Fields();
Download the Plugin
I have also added the above code to Github so you can download it as a plugin. This plugin will not be uploaded to the wordpress repository, so it will not receive any updates. As you download it, you will change the folder determination and plugin details to suit your needs.
I select growing mini plugins for code like this. By using code inside a plugin, it becomes easier to troubleshoot your website because you will quickly disable the snippets from your website. I actually have over 50 mini plugins on wpexplorer.com that do various tasks.
Conclusion
As you will see on the side of custom matter values throughout the search within wordpress is understated. I will certainly see some eventualities where it could be useful. Let me know in the comments while you have received problems or questions, however, and why you might be along with the code on your website. I would be curious to see some examples of the exact world. Thanks!
The article How to Add Custom Box Values in wordpress Seek appeared first on WPExplorer.
WP Support Plans
[ continue ]
wordpress Maintenance Plans | wordpress hosting
Learn more
wordpress-seek/”>Source link