So the generic field option values are post slugs, correct?
My form is:
[credform]
<div class="form-group">
<label>Choose a related resource</label>
[cred_generic_field type='select' field='related-resource' class="" urlparam=""]
{
"required":0,
"validate_format":0,
"persist":1,
"default":[],
"options":[ [wpv-view name="all-resources"] ]
}
[/cred_generic_field]
[cred_field field='related' force_type='taxonomy' output='bootstrap' class='form-control']
[cred_field field="related_popular" taxonomy="related" type="show_popular"]
</div>
[cred_field field='form_submit' output='bootstrap' value='Submit' class='btn btn-primary btn-lg']
[/credform]
so the options are my view "all-resources" which is just the JSON for the titles of the posts (not the slugs). Here is my view for the generic select ('related-resource'):
[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<wpv-loop>
[wpv-item index=1]{"value":"[wpv-post-id]","label":"[wpv-post-title]"}[wpv-item index=other],{"value":"[wpv-post-id]","label":"[wpv-post-title]"}
</wpv-loop>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-layout-end]
All I see in the generic select dropdown are the post titles but I assume the slug and the post-id is available also?
To get the value of the selected item in the callback, look for the generic field slug as a key in the $_POST superglobal. If the generic field slug is "my-generic-select" then you will find the selected value in $_POST['my-generic-select'];
Since my generic field slug is 'related-resource' I should use
$tag = $_POST['related-resource']
as i have already in my function to find the slug of the selected related resource post.
To convert that selected post slug to a post ID you can use the WordPress API get_page_by_path
should I use?...
//Returns the selected resource post id '$post_id_selected' with the slug $tag'
get_page_by_path('$tag', $post_id_selected, 'resource');
To get the post slug of the current post, use the WordPress API get_post().
$post_slug = get_post_field( 'post_name', get_post() );
So my final CRED action on submit should be:
/**
* add function for CRED form edit Resource with Related Resource
*/
function set_term_from_generic_select( $post_id, $form_data ) {
$forms = array( 10191 );
// first assign the selected slug to the current post 'related' taxonomy
if ( in_array( $form_data['id'], $forms ) ) {
$taxonomy = 'related';
$tag = $_POST['related-resource'];
wp_set_object_terms( $post_id, $tag, $taxonomy, true );
// then assign the current post slug to the selected post 'related' taxonomy to be bi-directional
get_page_by_path('$tag', $post_id_selected, 'resource');
$post_slug = get_post_field( 'post_name', get_post() );
wp_set_object_terms( $post_id_selected, $post_slug, $taxonomy, true );
}
}
add_action('cred_save_data', 'set_term_from_generic_select',100,2);
How am I doing so far?