Danstan Ongubo
Danstan Ongubo

Reputation: 105

How to return the title instead of the post ID in a WordPress Form Plugin Field

I have this custom hook from the Formidable plugin which gets the value from one field and copies it over to a second field.

More Details https://formidableforms.com/knowledgebase/frm_validate_field_entry/#kb-change-the-value-in-a-field

The code works as expected but instead of returning the exact copied value of the field, it displays it's ID.

I tried replacing $_POST['item_meta'][140] with get_the_title( $_POST['item_meta'][140] ); but now there's no value in the field.

add_filter('frm_validate_field_entry', 'copy_my_field', 10, 3);
function copy_my_field($errors, $posted_field, $posted_value){
  if ( $posted_field->id == 128 ) { //change 25 to the ID of the field to change
    $_POST['item_meta'][$posted_field->id] = $_POST['item_meta'][140]; 
//Change 20 to the ID of the field to copy  }
  return $errors;
}

Thanks.

Upvotes: 1

Views: 268

Answers (1)

Howard E
Howard E

Reputation: 5639

If I understand your question, and what you're saying... the $_POST['item_meta'][140] is the post ID of the post you want to find the title of?

This is not tested, but if $_POST['item_meta'][140] is the post ID, then this will get the title.

add_filter('frm_validate_field_entry', 'copy_my_field', 10, 3);
function copy_my_field($errors, $posted_field, $posted_value) {
    if ($posted_field->id == 128) {
        //get the post object from post ID
        $post = get_post($_POST['item_meta'][140]);
        $_POST['item_meta'][$posted_field->id] = $post->post_title;
    }
    return $errors;
}

Upvotes: 1

Related Questions