How to Use ACF Custom Fields in RSS Emails

Share this article:

How to Use ACF Custom Fields in RSS Emails | Hands of woman exchanging emails on a laptop - Juan Rojo Design Toronto

In order to use ACF custom fields in rss emails, you need to get familiarized with code. However, if you are using ChatGPT, you can arrive at the same answer as long as you provide detailed instructions on what you need.

I’m writing this article in case I need to refer back to it for future projects or someone else finds it useful.

When Would You Need to Use ACF Custom Fields In RSS Emails?

I first came across this request from a client planning to send job/opportunity notifications to their subscriber base. For instance, a new job gets posted to the directory for a specific position. The subscribing member will receive a daily email if any new opportunity matching their profile comes up. If nothing is available, no email will be sent.

That type of automation can be achieved using an RSS feed from their website. However, an email marketing platform will be required to complete the task.

“I still don’t see why you need to use a ACF custom field for this”. You would be right to make that statement if nothing more than the featured image, title and post link was all that you needed. In reality, that’s how most rss feeds are used on a regular basis. Think of people subscribing to blogs or online publishers.

What if you wanted to add something like location? If you have ever subscribed to a job board (i.e. Workopolis), you might notices that some of the jobs list the location for the listing. For instance, you could have something like Location: Toronto, ON or a full address like 255 Generic Rd, Toronto, ON.

Location field added to custom post type

How Do I Add A Custom Field to a WordPress RSS Feed?

If you need to add ACF custom fields in rss emails, you will need to add code to either the functions.php file (preferably in a child theme, otherwise you’ll need to add the snippet after each theme update) or inject it via code snippets.

In the image shown above for the location field, the address needs to be entered in full in order for the ACF field to generate a location on the map (along with coordinates).

As I mentioned before, the standard rss feed outputs the following fields for email marketing platforms:

  • Featured image
  • Post title
  • Link
  • Publication date

Adding the location field to the rss feed posed two main challenges:

  • Adding the custom field to the rss feed
  • Splitting the full array of the location field, so only one or two data points are shown

Let’s tackle the first part, adding a custom field to your rss feed. We will need to add the following code to functions.php or via code snippets:

function wpb_rsstutorial_customfield($content) {
global $wp_query;
$postid = $wp_query->post->ID;
$custom_metadata = get_post_meta($postid, 'my_custom_field', true);
if(is_feed()) {
if($custom_metadata !== '') {
// Display custom field data below content
$content = $content."<br /><br /><div>".$custom_metadata."</div>
";
}
else {
$content = $content;
}
}
return $content;
}
add_filter('the_excerpt_rss', 'wpb_rsstutorial_customfield');
add_filter('the_content', 'wpb_rsstutorial_customfield');

You will need to change the ‘my_custom_field‘ entry with your actual custom field.

Now, the code above is useful for regular text input field. What happens if you come cross an array that needs to be split into different portions and only show a couple of them. That’s the problem I was faced with when I had to get the location array, which looked something like this:

1234 Glenview Drive, Dallas, TX, USA, 32.823672, -97.2376226, 10, ChIJ1f9RQ7N5ToYRGKfpqpilMzQ, 1234 Glenview Dr, 1234, Glenview Drive, Glenview Dr, Dallas, Texas, TX, 76185, United States, US

How To Split The Array From A Custom ACF Location Field

As you can see in the array above, there’s too much information to pass to an email marketing platform. Is not that it cannot handle it, but it will add a lot of unnecessary information to the email.

For this particular case, I had to ask ChatGPT what the best course of action would be. After refining the instructions/prompt a couple of times, I got a snippet of code that produced the desired result:

// Add location to Jobs RSS feed.
function wpb_rsstutorial_customfield($content) {
    global $wp_query;
    $postid = $wp_query->post->ID;
    $custom_metadata = get_post_meta($postid, 'location', true);

    if (is_feed()) {
        // Check if the custom metadata is not empty
        if (!empty($custom_metadata)) {
            // Check if the custom metadata is an array
            if (is_array($custom_metadata)) {
                // Pick only the first two items from the array
                $first_two_items = array_slice($custom_metadata, 0, 1);
                // Convert the first two items array to a comma-separated string
                $custom_metadata_string = implode(', ', $first_two_items);
                // Append the modified string to the content
                $content .= "<div>" . $custom_metadata_string . "</div>";
            } else {
                // If it's not an array, append the custom metadata as it is
                $content .= "<div>" . $custom_metadata . "</div>";
            }
        }
    }
    return $content;
}
add_filter('the_excerpt_rss', 'wpb_rsstutorial_customfield');

The snippet of code above assumes that the custom field for location is actually called location. Remember the huge array that was being pulled before? Now it looks far more legible and simple: 1234 Glenview Drive, Dallas, TX, USA.

How Do I Add My Custom RSS Feed To My Email Marketing Platform?

All the major email marketing platforms let you create rss-based campaigns. However, the first thing you need to do is to get the url for your rss feed. In this particular case, we need the feed for your custom post type, which is automatically generated by WordPress:

yourwebsite.com/custom-category/accountant/feed?post_type=job

The URL above is telling your email marketing platform that it’s going to pull all the jobs posted under the category ‘accountant’. Therefore, whenever a new accounting job is posted, portal subscribers will receive an email notification. However, that automation assumes the user signed up to the portal as an accountant and nothing else.

Once the URL for the rss feed is added to your email campaign design, you might be able to do different things depending on the platform. In ActiveCampaign, you can style the title, as well as the excerpt from the feed. However, you could also craft a campaign which contains a featured image, a title linking back to the article and a button that does the same (just in case).