• Resolved robbiejackson

    (@robbiejackson)


    I’m developing a web site of walks, and have got a Walks custom post type pod to represent each walk. One of the fields is walk_grade which represents the grade of difficulty, and I’ve used a Field Type of Relationship related to a Grades custom taxonomy pod to represent that.

    I need to add the walks pod records programmatically to implement the migration from a previous system, and that’s where I’ve been having difficulties with setting the walk_grade relationship field.

    I’ve tried:

    $pod = pods('walk');
    $data = array(
        'post_title' => 'Test walk title',
        'post_content' => 'Short walk test description text',
        'town' => 'Townsville',
        'walk_grade' => 'Easy'    // tried it with and without this line
        );
    $id = $pod->add($data);     // this creates the pod with the title, content and town, but not grade
    $id = $pod->add_to('walk_grade', 'Easy');   // this doesn't work

    Instead of the add_to call updating the walk with the grade of difficulty I find that it’s created a new walks pod which is basically empty (and I get back a new $id).

    Could someone please point me in the right direction?

    Thanks,
    Robbie

    https://ww.wp.xz.cn/plugins/pods/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Jim True

    (@jimtrue)

    You sound like you’ve got flipped around. You shouldn’t use the walk_grade as a relationship field if it’s a taxonomy because that’s what it’s being treated as.

    add_to takes an ID, not a ‘value’

    http://pods.io/docs/code/pods/add-to/

    So you’d need to get either the term_id of your taxonomy or the post id for the relationship.

    if you associate walk_grade as your taxonomy with the walk post type, does the walk_grade => ‘Easy’ work?

    Thread Starter robbiejackson

    (@robbiejackson)

    Many thanks for your help Jim, I think I’ve got it sorted now. I just read the add_to ($field, $value) signature and didn’t read the parameter definition closely enough!

    In case it helps anyone else, I found the id which I needed using

    $grade = pods('grade');
    $params = array('where' => "name = 'Easy'");
    $grade->find($params);
    $result = $grade->fetch();
    $easy_id = $result['term_id'];

    then

    $pod = pods('walk');
    $data = array(
        'post_title' => 'Test walk title',
        'post_content' => 'Short walk test description text',
        'town' => 'Townsville',
        'walk_grade' => $easy_id
        );
    $id = $pod->add($data);     // this creates the walks pod, including the grade ok
    $id = $pod->add_to('walk_grade', $easy_id);   // inserts the appropriate wp_pods_rel relationship record

    I still get the blank Walks pod record created as a result of the add_to() call, but I can easily remove that with a subsequent delete() call.

    Robbie

    Plugin Contributor Jim True

    (@jimtrue)

    I think add_to is intended to be used only when you’re adding a new relationship to an existing relationship field (ie like push’ing a value onto an array).

    If this is the only possible ‘grade’ for this walk, just use $pod->add() to save your record to ‘walk’ and the relationship field will be set automatically as part of adding that record. I believe you’re actually saving the relationship by setting it in your ‘walk_grade’ => $easy_id.

    You don’t have to then turn around and add it again.

    Thread Starter robbiejackson

    (@robbiejackson)

    Jim, I’ve checked the wp_podsrel records and you’re quite correct, the call to add() sets the appropriate record there as well.

    So the call to add_to() is superfluous. And taking this out would remove that blank Walks record that appears when it’s called.

    Many thanks again,

    Robbie

    Plugin Contributor Jim True

    (@jimtrue)

    You’re quite welcome. I had a feeling it looked like you were doing a double-call.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Programmatically adding a pod with a relationship field’ is closed to new replies.