WPSyncSheets For Core

Documentation

Custom Meta Header Filters for Import and Export– WPSyncSheets For Core

These filters allow modification of custom meta header values during export and import. They are useful when values need to be displayed differently in the sheet than how they are stored in the database.

For example:

  • Showing taxonomy term names instead of term IDs in the sheet
  • Converting term names back to IDs during import
  • Formatting custom meta values
  • Handling ACF fields in a custom way

This example converts taxonomy term IDs to term names during export and converts them back to IDs during import.


Example: Export Term Names Instead of Term IDs

This example converts the stored term IDs into readable term names before sending them to the sheet.

add_filter('wpss_core_static_header_meta_value', 'wpss_core_static_header_meta_value', 10, 3);

function wpss_core_static_header_meta_value($meta_value, $post_id, $meta_key) {

    // Get meta value if empty
    if (empty($meta_value)) {
        $meta_value = get_post_meta($post_id, $meta_key, true);
    }

    if (empty($meta_value)) {
        return $meta_value;
    }

    // Apply only for specific meta keys
    if ('client_name' === $meta_key || 'client_nationality' === $meta_key) {

        // Map meta key to taxonomy
        $acf_taxonomy_map = array(
            'client_name'        => 'client',
            'client_nationality' => 'nationality',
        );

        $taxonomy = $acf_taxonomy_map[$meta_key];

        // Handle multiple values
        if (is_array($meta_value)) {

            $terms = array();

            foreach ($meta_value as $term_id) {

                $term = get_term($term_id, $taxonomy);

                if ($term && !is_wp_error($term)) {
                    $terms[] = $term->name;
                }
            }

            if (!empty($terms)) {
                $meta_value = implode(', ', $terms);
            }

        } else {

            $term_array = explode(',', $meta_value);
            $terms = array();

            foreach ($term_array as $term_id) {

                $term = get_term(trim($term_id), $taxonomy);

                if ($term && !is_wp_error($term)) {
                    $terms[] = $term->name;
                }
            }

            if (!empty($terms)) {
                $meta_value = implode(', ', $terms);
            }
        }
    }

    return $meta_value;
}

Example Result

Client Field (client_name → client taxonomy)

Stored in DatabaseShown in Sheet
23John Smith
12,15Alice Brown, Michael Lee

Nationality Field (client_nationality → nationality taxonomy)

Stored in DatabaseShown in Sheet
5German
3,8French, Italian

Example: Import Term Names and Convert to Term IDs

This example converts term names from the sheet back into term IDs before saving.

add_filter('wpss_core_static_header_meta_import_value', 'wpss_core_static_header_meta_import_value', 10, 3);

function wpss_core_static_header_meta_import_value($meta_value, $post_id, $meta_key) {

    if (empty($meta_value)) {
        return $meta_value;
    }

    // Apply only for specific meta keys
    if ('client_name' === $meta_key || 'client_nationality' === $meta_key) {

        // Map meta key to taxonomy
        $acf_taxonomy_map = array(
            'client_name'        => 'client',
            'client_nationality' => 'nationality',
        );

        $taxonomy = $acf_taxonomy_map[$meta_key];

        // If already numeric, assume ID
        if (is_numeric($meta_value)) {
            return $meta_value;
        }

        $term_names = explode(',', $meta_value);
        $term_ids   = array();

        foreach ($term_names as $name) {

            $term = get_term_by('name', trim($name), $taxonomy);

            if ($term && !is_wp_error($term)) {
                $term_ids[] = $term->term_id;
            }
        }

        if (!empty($term_ids)) {
            $meta_value = isset($term_ids[0]) ? $term_ids[0] : '';
        }
    }

    return $meta_value;
}

Import Behavior

During import, the values are converted back before saving to the database.

Client

Sheet ValueStored in Database
John Smith23
Alice Brown, Michael Lee12,15

Nationality

Sheet ValueStored in Database
German5
French, Italian3,8

Filter Parameters:

Both filters provide the following parameters:

ParameterDescription
$post_idThe post ID being processed
$meta_valueThe current meta value. This is the value being exported or imported.
$meta_keyThe meta key used to store the data in the database (post meta key).

Where to Add This Code

This code can be added in:

  • Snippets plugin
  • Theme functions.php
  • Custom plugin

Real-Time Import, Export, and Migration of WordPress Posts, Pages & Users.

WPSyncSheets For Core

5.0
View Plugin

Have a question?

We’re here to help, just send us a message.

Need help?

We're ready to help out & answer questions!