Can I search inside custom fields generated from ACF plugin?

[ad_1]

Sure, it’s simple.

Since we have no good UI for this, you can add a small code:


add_filter('wpfts_index_post', function($index, $post)
{
    global $wpfts_core;

    if ($post && $wpfts_core) {
        // Example for ACF fields
        // 1. Check if the post has correct post_type
        if ($post->post_type == 'my_post_type2') { // Use YOURS here
             // 2. Check if ACF is active!
             if (function_exists('get_field')) {
                 // 3. Get ACF field value
                 $t = get_field('my_acf_field', $post->ID); // Use YOURS here
                 // 4. Store to index
                 $index['my_cluster2'] = trim($t); // YOUR cluster name
                 // Repeat 3 & 4 if you need to index more ACF fields
             }
        }
     }
 
     // Return summarized $index to put it to database
     return $index;
}, 3, 2);

Put this code to your functions.php of the child theme and rebuild index.

You can use Sandbox Area Tester to test the wpfts_index_post hook without need to rebuild index just to be sure everything is indexed.

Hope this helps.

Thanks for the question.

This is so cool thanks! Just another question..what if I upload the document/pdf inside ACF File field. Will it still search inside that field?

Yes, it will, however for search inside the files you will need the pro version.
You can get one for free here

The respective code will be:


// 2. Get the url of the attached file (set "url" in the ACF field settings)
$url = get_field('my_attached_file', $post->ID);
if (strlen($url) > 0) {
    // 3. Include WPFTS's Pro library
    require_once $wpfts_core->root_dir.'/includes/wpfts_utils.class.php';

    // 4. Extract file content
    $ret = WPFTS_Utils::GetCachedFileContent_ByLocalLink($url);

    // 5. Store to index
    $index['my_cluster3'] = isset($ret['post_content']) ? trim($ret['post_content']) : '';
}

This code should be inserted after the “// Repeat 3 & 4 if you need to index more ACF fields” line of the previous snippet.

Hope this helps!

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer