1

It's a little hard to explain but I suppose you can understand if I elaborate it even more...

Normally when you search on a WordPress site, the results are either based on Title, Content, Category, Tags.

I want my Search to go through posts and look at custom fields too... So if a user searches for a specific custom field, let's say I have the post titled "Blah" and I created a field name is "Chocolate" and the field content is "Cadbury" so when user Search's for "Cadbury" the post "Blah" Should show up... I know there are few plugins for that like <http://wordpress.org/plugins/relevanssi/> but I'd be better off not bloating my site with too many plugins and plugins for every little thing...

I am not so great with WordPress but I suppose wp_query may do the work... Than you for any and every help

  • 1
    Actually Relevanssi may be a better choice here rather than trying to code something yourself. Native custom field queries are very slow, and trying to search all custom fields will probably grind your server to a halt. – Milo Apr 27 '18 at 3:26
0

There are few ways to solve this problem...

1. Don't bother and use Relevansi (or some other similar plugin)

You can often hear that using many plugins is a bad thing. It's not entirely true. It's bad only if these plugins have big impact on use of resources. In this case, Relevansi won't cause such impact.

Of course there is another problem with plugins - you have to maintain them (updates, and so on).

2. Use posts_join and posts_where hooks

It's the easiest way of solving your problem. Just join the posts and postmeta tables and add WHERE clauses that will search your value in proper custom fields.

// Join with postmeta table
function mycf_search_join( $join ) {
    global $wpdb;

    if ( is_search() ) {    
        $join .= " LEFT JOIN {$wpdb->postmeta} mycfpm ON ({$wpdb->posts}.ID = mycfpm.post_id ";
    }

    return $join;
}
add_filter('posts_join', 'mycf_search_join' );

// Add where clauses
function mycf_search_where( $where ) {
    global $wpdb;

    if ( is_search() ) {
        $where = preg_replace(
            "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
            "({$wpdb->posts}.post_title LIKE $1) OR (mycfpm.meta_value LIKE $1)", $where );
    }

    return $where;
}
add_filter( 'posts_where', 'mycf_search_where' );

// Prevent duplicates
function cf_search_distinct( $distinct ) {
    global $wpdb;

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

    return $distinct;
}
add_filter( 'posts_distinct', 'mycf_search_distinct' );

The only problem with this solution is that it can be a little bit slow. Especially if you don't want to search in all custom fields.

3. Custom CF indexing

You can also use save_post hook to create something like an index. After saving post you get all custom fields that should be searchable, combine their values and save as another custom field.

So let's say there is a post with CFs: color=red, location=europe, name=john and all of them should be searchable. You can get these values, separate them with some separator char/sequence (for example #, so you'll get red#europe#john) and save this string as new CF (let's call it _cf_search_string).

function mycf_prepare_search_index_cf( $post_id ) {
    if ( wp_is_post_revision( $post_id ) ) return;

    $meta_keys = array('cf_key1', 'cf_key2', 'cf_key3');
    $search_string = '';
    foreach ( $meta_keys as $k ) {
        $search_string .= get_post_meta( $post_id, $k, true ) . '#';
    }
    update_post_meta( $post_id, '_cf_search_string', $search_string );
}
add_action( 'save_post', 'mycf_prepare_search_index_cf', 100, 1 );

Then you can use code from 2. to search only in this one custom field and not all of them, so it will add only one join and won't multiply rows that will be searched during DB query.

  • Wow... Thnx @Krzysiek Dróżdż for the detailed Answer... I'll try the 3rd Method and if that doesn't work I guess I'll work with the plugin. Since the second as you said might be slow. But I'll still give the second method a try. Again, thank you much for the help. – Edogawa Kudo Apr 27 '18 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.