wordpress-custom-field-search

How to Adding Custom Fields as Search Criteria in WordPress

WordPress has features that allows the user to input data in a specific format that is custom field. Custom fields that make wordpress feels special and can improve its ability, in addition to the plugin of course. The use of custom fields in wordpress really flexible. You can fill in any custom field you need, then access it. Your content will be more rich and complete.

Also read WordPress blog that you should follow in 2015

After filling in a custom field, you may want to search for content based on custom fields. For example the price, you want to classify products based on price, product clothes at a price below USD $ 20. This is a problem that we need to solve. Because, the search feature on wordpress can not do things like that.

Try to use Plugin

I’m looking for some plugins that can do as I wish above, there are several plugins that can be tried, among others:

1. Custom field search by bestwebsoft

This plugin allows you to add custom website search field in any post. It comes with a list of custom fields. All you need is to choose which of them you want to see in search. Make your site useful and effective with the Custom Field Search Plugin.

Screenshot

Custom Fields Search by BestWebSoft

You can download it here or from wordpress dashboard.

2. WP Custom field Search

This is a search plugin for wordpress, designed to filter posts in a more structured way than the default wordpress search. Specifically it allows multiple form inputs to be configured to search different aspects of a post, i.e. one term could search the post content, one the post title and one could search a custom field associated with the post.

Screenshot

customfield search wordpress

You can download it here or via wordpress dashboard.

3. SearchWP

Is a premium plugin that has more capabilities, easier to customize. SearchWP was built to be a plugin you can install and fully utilize instantly after activating. It was also designed to accommodate developers in every way possible, allowing for very fine-grained control & customization.

You can buy it here (no aff link)

But, I am not satisfied with the use of plugins. Too many plugins will result into a heavy load website. Especially if your site has a lot of the end. It is not recommended to use the plugin.

So, What is your Solutions?

Then, I try to find other solutions that add the search feature on wordpress with a hook. Essentially enter postmeta column on the search category. Example code like the following

<?php
/**
 * Extend WordPress search to include custom fields
 *
 * http://adambalee.com
 */

/**
 * Join posts and postmeta tables
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
 */
function cf_search_join( $join ) {
    global $wpdb;

    if ( is_search() ) {    
        $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
    }
    
    return $join;
}
add_filter('posts_join', 'cf_search_join' );

/**
 * Modify the search query with posts_where
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where
 */
function cf_search_where( $where ) {
    global $pagenow, $wpdb;
   
    if ( is_search() ) {
        $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', 'cf_search_where' );

/**
 * Prevent duplicates
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_distinct
 */
function cf_search_distinct( $where ) {
    global $wpdb;

    if ( is_search() ) {
        return "DISTINCT";
    }

    return $where;
}
add_filter( 'posts_distinct', 'cf_search_distinct' );
?>

Source

Just add these codes to themes functions.php

Conclusion

WordPress is a CMS that is easy to be developed in accordance with the wishes and our needs. If you want more capabilities than wordpress, look for some code for it, if you are not familiar with the code, then use the plugin. Evidently, we can add the search feature capabilities of wordpress just by adding a few lines of code. Simple

If you do? share your experience with wordpress in the comments field

 

No Responses

Show all responses

Write a response