Documentation

View the installation guide, browse the Knowledge Base, find out about SearchWP's many hooks, get support

How to give bonus weight to Advanced Custom Fields (ACF) Checkbox Fields

Advanced Custom Fields is a terrific plugin that makes customizing your site very easy. Not only does it facilitate content entry, it can also help you make management of other details easier for content managers. Utilizing both ACF and SearchWP is something many customers take advantage of!

One thing you can do is set up ACF with a checkbox field that controls whether some attribute of the current post is true or false. For example, perhaps some of your posts are considered proprietary and you’d like to ensure that those posts are given more weight when searches are performed.

Using a couple of SearchWP’s hooks that’s easily accomplished!

Assuming your ACF Checkbox field has a name of proprietary you can add this to your theme’s functions.php and a bonus weight of 1000 will be added to any post that has that Advanced Custom Field Checkbox ticked:

<?php
add_filter( 'searchwp_query_join', function( $sql, $engine ) {
global $wpdb;
$my_meta_key = 'proprietary';
return $sql . " LEFT JOIN {$wpdb->postmeta} AS swp9633meta ON {$wpdb->posts}.ID = swp9633meta.post_id AND swp9633meta.meta_key = '{$my_meta_key}'";
}, 10, 2 );
add_filter( 'searchwp_weight_mods', function( $sql ) {
$modifier = 1000;
return $sql .= " + IF(swp9633meta.meta_value+0 = 1, {$modifier}, 0)";
} );
view raw functions.php hosted with ❤ by GitHub

You can customize the hooks to use any ACF field name by editing line 6 with the applicable field name, and if you’d like to adjust the bonus weight that’s used you can edit line 12.

Fix keyword search on your site. No coding required!

Now you can utilize all of the content that's gone unrecognized by native WordPress keyword search instantly with SearchWP

Get SearchWP