How to Connect WordPress with Lucidea (Presto)

Table of Contents

How to Connect WordPress with Lucidea (Presto)
Advanced Knowledge Management XML Data Using ACF: A Step-by-Step Guide

If you’re working with WordPress and need to import XML data into custom fields, this guide will show you how to do it using Advanced Custom Fields (ACF). You’ll learn how to:

  1. Set up ACF fields to store XML data.
  2. Process XML data and map it to these fields in WordPress.
  3. Automatically create relationships between posts based on the imported data.

This tutorial will focus on a practical example: connecting data from the City of Presto’s XML feed to WordPress. By the end, you’ll have a clear understanding of how to set up the necessary fields, write the required code, and automate the process.

What You’ll Learn

In this guide, you’ll cover:

  • Creating ACF fields: Learn how to set up text and relationship fields to store imported data.
  • Using functions.php: Understand the role of the functions.php file in custom functionality.
  • Processing XML with WordPress: Write a function that reads XML data and updates your ACF fields.
  • Creating relationships: Automate bidirectional relationships between posts based on the imported data.

Step-by-Step Overview

  1. Setting Up ACF Fields

You’ll start by creating ACF fields for the XML data:

  • Text field for storing titles.
  • Relationship field for connecting related posts.

These fields act as containers for your XML data and relationships.

  1. Writing the Code

In your theme’s functions.php file, you’ll add code to:

  • Automatically trigger ACF updates when new XML data is imported.
  • Read XML data and populate the title field.
  • Create or update related posts and link them through the relationship field.
  1. Processing XML Data

Using the pmxi_saved_post hook, you’ll sync the XML data to WordPress. A helper function will check if a related post exists based on the imported title. If it doesn’t exist, it will create a new one.

  1. Creating Relationships

Relationships allow you to connect imported data to other posts. For example, if an imported record references another study, the relationship field will link the two posts automatically.

How the Code Works

Here’s a breakdown of the key code snippets:

  1. Triggering ACF Updates:
				
					add_action('pmxi_saved_post', function ($post_id) {
    do_action('acf/save_post', $post_id);
});

				
			

This hook ensures ACF fields are updated every time a new XML record is imported.

2. Processing XML Data:

				
					function process_city_of_presto_data($post_id) {
    // Skip autosaves
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;

    // Ensure the post type matches your custom post type
    $post_type = get_post_type($post_id);
    if ($post_type !== 'your_custom_post_type') return;

    // Process ACF fields
    $xml_title = get_field('title', $post_id);
    if ($xml_title) {
        $related_post_id = find_or_create_post($xml_title);
        update_relationship_field($post_id, $related_post_id);
    }
}

				
			

This function extracts the title from the XML and updates the relationship field.

3. Creating or Finding Related Posts:

				
					function find_or_create_post($title) {
    $existing_post = get_posts([
        'post_type' => 'your_custom_post_type',
        'title' => $title,
        'numberposts' => 1,
    ]);

    if (!empty($existing_post)) {
        return $existing_post[0]->ID;
    }

    return wp_insert_post([
        'post_type' => 'your_custom_post_type',
        'post_title' => $title,
        'post_status' => 'publish',
    ]);
}

				
			

This helper function checks if a post with the imported title exists. If it doesn’t, it creates a new one.

4. Updating the Relationship Field:

				
					function update_relationship_field($post_id, $related_post_id) {
    $related_items = get_field('related_items', $post_id) ?: [];
    if (!in_array($related_post_id, $related_items)) {
        $related_items[] = $related_post_id;
        update_field('related_items', $related_items, $post_id);
    }
}

				
			

This function ensures the relationship field is updated to include the related post.

Why This Works

The key to connecting WordPress with XML is understanding the flow:

  1. XML Import: The XML file provides the data.
  2. ACF Fields: These store the imported data in WordPress.
  3. Hooks: The pmxi_saved_post and acf/save_post hooks process the XML and update the fields.
  4. Relationships: Helper functions create or find related posts and establish connections.

This setup is flexible, so you can adapt it to your specific data structure and requirements.

Full Code Example: Connecting WordPress with XML Data Using ACF

Below is the complete code that you can add to your WordPress theme’s functions.php file. It includes all the functions discussed in this guide, ready to use:

				
					// Trigger ACF save_post action when WP All Import saves a post
add_action('pmxi_saved_post', function ($post_id) {
    do_action('acf/save_post', $post_id);
});

// Sync relationships and process XML data after ACF updates
add_action('acf/save_post', 'process_city_of_presto_data', 20);

function process_city_of_presto_data($post_id) {
    // Skip autosaves
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;

    // Ensure this only applies to your specific post type
    $post_type = get_post_type($post_id);
    if ($post_type !== 'your_custom_post_type') return;

    // Get the title from the XML
    $xml_title = get_field('title', $post_id);
    if ($xml_title) {
        // Find or create the related post
        $related_post_id = find_or_create_post($xml_title);

        // Update the relationship field
        update_relationship_field($post_id, $related_post_id);
    }
}

function find_or_create_post($title) {
    // Search for an existing post with the same title
    $existing_post = get_posts([
        'post_type' => 'your_custom_post_type',
        'title' => $title,
        'numberposts' => 1,
    ]);

    // If found, return the post ID
    if (!empty($existing_post)) {
        return $existing_post[0]->ID;
    }

    // Otherwise, create a new post and return its ID
    return wp_insert_post([
        'post_type' => 'your_custom_post_type',
        'post_title' => $title,
        'post_status' => 'publish',
    ]);
}

function update_relationship_field($post_id, $related_post_id) {
    // Get the current relationship field values
    $related_items = get_field('related_items', $post_id) ?: [];

    // Add the related post if it's not already included
    if (!in_array($related_post_id, $related_items)) {
        $related_items[] = $related_post_id;
        update_field('related_items', $related_items, $post_id);
    }
}

				
			

Final Thoughts

This code provides a simple yet powerful way to connect WordPress posts with XML data using ACF. It creates or finds related posts, updates ACF fields, and ensures bidirectional relationships are established.

Feel free to copy and paste the code above into your project. With the explanations provided, you now have the understanding to modify it for your specific needs.

Happy coding, and welcome to the world of seamless WordPress integrations! 

Picture of Packy Savvenas
Packy Savvenas

Packy Savvenas is an 10X web designer, committed to elevating your online business to achieve its ambitious targets! By transforming and enhancing websites, he promises to broaden your customer base and dramatically increase your product sales. Prepare to upgrade your online presence and watch your business take flight!

Recent Articles

How to Connect WordPress with Lucidea (Presto)
Why Starting Local is the Foundation for Nationwide SEO Success
How Strategic SEO Transforms Your Online Presence: A Case Study
AI: The Future of Job Enhancement, Not Replacement
Power of the Web Named Among Top 13 Web Design Firms in Minneapolis