• Resolved junkes

    (@junkes)


    I have a CPT called “Song” and another one called “Movieclip”. I have a bidirectional relationship field connecting them. Is it possible to populate all informations in Movieclip CPT with informations from connected song without having to do it twice?

    Example: in a Song editing page when I create a new movieclip post to connect with it, I fulfill only the new infos and the already known infos like artist or whatever are dinamically populated with the infos of the connected song.

    Is that possible with Pods?

    • This topic was modified 1 month, 1 week ago by junkes.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Paul Clark

    (@pdclark)

    When two post types are connected through a bidirectional relationship, the fields on the related posts become available through dot . notation.

    So if field related_song connects to Pod songs from Pod movieclip, magic tag {@related_song.post_title} will display the value of Post Title from related_song when viewing movieclip.

    For more advanced queries, such as in PHP, the relationship is stored in the database as either one related ID or multiple rows of related IDs. The ID of the related object(s) can then be used to lookup additional data.

    Thread Starter junkes

    (@junkes)

    Thanks for the answers, @pdclark.

    About displaying, it’s ok, all functional and great. But I wanted it to be stored in the database. I can do it with normal custom fields, but not with the relationship ones. Can you be more specific on that?

    On docs.pods, you’ve got two examples of updating post_title with custom fields and it’s great but I couldn’t make it work for relationship fields.

    Plugin Support Paul Clark

    (@pdclark)

    See the Pods post_save hook and pods()->save() method.

    One would hook to the save action of post_type_a, get the IDs from the relationship field, then use ->save() to update values in post_type_b.

    ->save() takes a keyed array, where the keys are the field names and the values are the field values.

    Most field values will be text, while a list or relationship field will be an array of text values.

    <?php 
    add_action(
    'pods_api_post_save_pod_item',
    function ( $pieces, $is_new_item, $id ) {

    $post_type_being_saved = 'song';

    if ( $post_type_being_saved === $pieces['params']->pod ) {

    // Field "related_movieclips" on song relating to multiple movieclips.
    foreach( (array) $pieces['fields']['related_movieclips']['value'] as $movieclip_id ) {

    pods(
    'movieclip', // IDs relate to a Pod named 'movieclip'.
    $movieclip_id // The ID being updated. Assumes these are related by the field.
    )->save(
    [
    // Keys are names of fields on movieclip.
    // Values are either text or arrays to be saved to movieclip.
    // $pieces['fields'][ ...field_name... ]['value'] contains saved song field values.
    'name_of_a_movieclip_field' => @$pieces['fields']['field_from_song']['value'],
    'repeating_field_on_movieclip' => [
    'Value 1',
    'Value 2',
    'Value 3',
    ],
    'relationship_field_of_ids' => [
    123,
    456,
    789,
    ],
    ]
    );

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