#Adding ACF Custom Fields to Context/Embeddings etc

1 messages · Page 1 of 1 (latest)

clever root
#

This is a fairly basic integration to get AI Engine to load the content of ACF custom filed(s) in embeddings sync, context, data set builders and anywhere else it takes the content of a post.

It may not be perfect (I'm sure someone will pick up a way it can be done differently) but this works, including for post-types that don't have the custom field (so it ignores it and uses the content as usual).


function add_acf_mwai( $content, $postId ) {
    $custom_filed_1 = get_field( 'your_custom_filed_ID', $postId ); // Get the value of the a ACF custom filed - replace 'your_custom_filed_ID' with the name of the field you wish to use
    if ( $custom_filed_1) {
        $content .= $your_custom_filed_ID; // Append the value of the 'your_custom_filed_ID' to the post content
    }
    return $content;
}```

This version should work if you need multiple ACF fields to be appended:

```add_filter( 'mwai_pre_post_content', 'add_acf_fields', 10, 2 );

function add_acf_fields( $content, $postId ) {
    $field_1 = get_field( 'ACF_Field_1', $postId );
    $field_2 = get_field( 'ACF_Field_2', $postId );

    if ( $field_1 ) {
        $content .= '<p>' . $field_1 . '</p>'; // Append the value of ACF_Field_1 to the post content
    }

    if ( $field_2 ) {
        $content .= '<p>' . $field_2 . '</p>'; // Append the value of ACF_Field_2 to the post content
    }

    return $content;
}```
hidden pelican
#

Thank you very much!! I find it very interesting to link this plugin with Advanced Custom Fields. In the coming days, I will try to test these functions, especially with the ACF options page. Great job!

lean harness
clever root
lean harness
lean harness
#

Just FYI, we edited the code so that it looks up how many fields are on the page and then collects them all. We tested it and it seems to work.


function add_all_acf_fields( $content, $postId ) {
    $fields = get_fields( $postId ); // Get all ACF fields for the post
    
    foreach ( $fields as $field_name => $field_value ) {
        if ( $field_value ) {
            $content .= '<p>' . $field_value . '</p>'; // Append the value of the field to the post content
        }
    }

    return $content;
}```
cyan lintel
#

Is anyone kind enough to do a video (or loom) of how it is working? We are trying to create a separate form (with ACF or Formidable Form) where people will be able to select parameters (Like Target audience, or Write Style) that will be include in the prompt of AI From Submit.

keen lion
clever root
# cyan lintel Is anyone kind enough to do a video (or loom) of how it is working? We are tryin...

You can include parameters in the AI forms blocks within the plugin that you can then refrence in the prompt. This snippet is about allowing AI Engine to 'see' the content of ACF filed when it is creating embeddings, fine tuning or using the {CONTENT} tag. BUT, it won't allow the chatbot to identify which value came from which filed, so you wouldn't be able to use this snippet to select and apply parameters.

gilded raft
# cyan lintel Is anyone kind enough to do a video (or loom) of how it is working? We are tryin...

I am trying to fiqure out if this thread is working out the solution I am looking for. Here is what I am thinking. A user can fill fields on a "universal" form (like a profile for instance) that saves the inputs to their user data. Then when they use forms on the website, this data can be called upon and included in the prompts.

So I can have a form where a users name, email or other field is automatically placed in the prompt with something like {USER_EMAIL}?

Is this what we are trying to do here?

clever root
#

No, this isn't what this snippet is for. This allows AI engine to see the fields created using the ACF plugin which are often used in custom posts etc.

You may be able to adapt this to retrieve the data if you store it in the user meta but this would still also give the bot the other content of the page.

indigo hull
#

Thanks for the information. But how do I use the code? Do I just put it in any files? I am new to this. 😂

clever root
exotic siren
hidden pelican
#

hey guys, i really struggle with these afc fields... i used the codes you are sharing and used them and also tried to modify them with chatgpt. right now when i refresh a post under embeddings it only shows the fields names but nothing else.... any ideas?

jagged schooner
# lean harness Just FYI, we edited the code so that it looks up how many fields are on the page...

I have made a small adjustment to your code to make it easier for GPT to recognize the fields and distinguish between each element:

add_filter( 'mwai_pre_post_content', 'add_all_acf_fields', 10, 2 );

function add_all_acf_fields( $content, $postId ) {
    $fields = get_fields( $postId ); // Get all ACF fields for the post
    
    foreach ( $fields as $field_name => $field_value ) {
        if ( $field_value ) {
            $content .='<p>' . $field_name . ': ' . $field_value . '</p></br>'; // Append the value of the field to the post content
        }
    }

    return $content;
}```
lime ferry
#

hello i how can i add file upload at the frontend

pure sierra
#

I had to edit the code a bit to make it work in my case. If anyone needs :

add_filter('mwai_pre_post_content', function($content, $postId) {

// Get our ACF fields
$acf_fields = get_fields($postId);

if ($acf_fields) {
    foreach ($acf_fields as $field_name => $field_value) {
        if (is_array($field_value)) {
            // Convert the table into a readable string
            $field_value = implode(', ', array_map(function($item) {
                return is_array($item) ? implode(', ', $item) : esc_html($item);
            }, $field_value));
        } else {
            $field_value = esc_html($field_value);
        }
        $content .= "\n<p><strong>" . esc_html($field_name) . ":</strong> " . $field_value . "</p>";
    }
} else {
    error_log('No ACF fields found for this post: ' . $postId);
}

return $content;

}, 10, 2);

dusty urchin
#

This works in my case:
add_filter('mwai_pre_post_content', function($content, $postId) {
if (!function_exists('get_fields')) {
return $content;
}

$acf_fields = get_fields($postId);

if (!$acf_fields || !is_array($acf_fields)) {
    return $content;
}

$acf_content = "\n\n--- ACF Custom Fields ---\n";

foreach ($acf_fields as $field_name => $field_value) {
    if (empty($field_value)) {
        continue;
    }
   
    $formatted_value = '';
   
    if (is_array($field_value)) {
        $formatted_value = format_acf_array($field_value);
    } elseif (is_object($field_value)) {
        if (isset($field_value->post_title)) {
            $formatted_value = $field_value->post_title;
        } elseif (isset($field_value->display_name)) {
            $formatted_value = $field_value->display_name;
        } else {
            $formatted_value = print_r($field_value, true);
        }
    } else {
        $formatted_value = strip_tags($field_value);
    }
   
    if (!empty($formatted_value)) {
        $acf_content .= $field_name . ": " . $formatted_value . "\n";
    }
}

return $content . $acf_content;

}, 10, 2);

function format_acf_array($array, $depth = 0) {
if ($depth > 3) return '';

$output = '';
foreach ($array as $key => $value) {
    if (is_array($value)) {
        $output .= "\n" . str_repeat('  ', $depth) . $key . ": " . format_acf_array($value, $depth + 1);
    } elseif (is_object($value)) {
        if (isset($value->post_title)) {
            $output .= $value->post_title . ', ';
        }
    } else {
        $output .= strip_tags($value) . ', ';
    }
}
return rtrim($output, ', ');

}