• Resolved Reed Sutton

    (@reedus33)


    Is it possible to import a CSV into a custom post relationship field? Would this be done by having the values in the cell comma separated or another way?

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Paul Clark

    (@pdclark)

    Yes. The challenge is that a CSV is a two-dimensional data type: it contains data and rows.

    As you mentioned, the additional dimension required for multiple values within a single relationship field is usually annotated with a special character within the CSV field, like a comma (quoted/escaped within the field), pipe |, or new line within the field.

    The PHP function for processing a CSV file is fgetcsv().

    The PHP function for converting text, such as 11,2,3,5,8,13 into an array (list) datatype is explode() and possibly intval() for ensuring IDs are integers, not strings (text).

    The Pods method for saving content or fields, including relationship fields, is ->save(), for example:

    <?php // @see https://docs.pods.io/code/pods/save/
    $pod = pods( 'post', 123 ); // 123 = ID of a post to write to.
    $pod->save(
    [
    // An array where keys are the field name.
    'relationship_field' => [
    // A relationship field is an array of IDs.
    11, 2, 3, 5, 8, 13
    ],
    // Core fields and taxonomy connections are supported.
    'post_content' => '<p>Some content...</p>',
    ]
    );
    Thread Starter Reed Sutton

    (@reedus33)

    So I already manage my Pods data by importing a published google sheet (csv) into wordpress using WP All Import.

    So I guess my question is more around, with the native behavior of Pods, will a cell with multiple values comma separated, properly import into a simple relationship field that has options corresponding to those values?

    Plugin Support Paul Clark

    (@pdclark)

    It would be similar to https://www.wpallimport.com/documentation/code-snippets/#how-to-import-jetengine-relations

    Import the comma-separated field to a temporary field key, such as _temp_rel_field.
    Add code similar to the below example All Import > Settings > Function Editor

    <?php
    /**
    * Convert field containing comma-separated relationships IDs to a Pods relationship.
    *
    * @see https://www.wpallimport.com/documentation/code-snippets/#how-to-import-jetengine-relations
    */
    add_action(
    'pmxi_saved_post',
    function( $id ) {
    $temporary_field_key = '_temp_rel_field';
    $real_field_key = 'rel_field';
    $post_type = 'post';

    // Convert comma-separated IDs to array of integers.
    $temporary_field_value = get_post_meta( $id, $temporary_field_key, true );
    $value_as_array = empty( $temporary_field_value )
    ? [] // Default if empty.
    : array_map(
    function( $related_id ) {
    // Remove whitespace and assure the ID is an integer.
    return intval( trim( $related_id ) );
    },
    explode( ',', $temporary_field_value ) // Comma-separated.
    );

    // Save the relationship field.
    pods( $post_type, $id )->save(
    [
    // Key is the name of the field, configured above.
    // Value is an array of IDs stored as integers.
    $real_field_key => $value_as_array,
    ]
    );

    // Remove temporary data.
    delete_post_meta( $id, $temporary_field_key );
    },
    10,
    1
    );

    Above assumes IDs are imported literally in the import file.

    If IDs have changed, looking up IDs according to another reference, such as comma-separated slugs, may be more appropriate.

    e.g., Import file stores slugs, array_map() looks up ID from slug, then returns array of IDs. Such an approach may be more appropriate for a post-import hook if the relationships are to the post type being imported.

    If no post-import hook is available, one could also query all posts with the temporary field, then run manually after the import.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.