Problem: I would like to use a CRED generic field to display users, and modify the author of the created post to match the selected user.
Solution: Using a View to create options for a generic field is not a supported feature of Views. The steps other users have had luck with are described below, but not guaranteed to work.
Create a View of all Users:
[wpv-layout-start] [wpv-items-found] <!-- wpv-loop-start --> <wpv-loop> [wpv-item index=1] {"value":"[wpv-user field="ID"]","label":"[wpv-user field="display_name"]"} [wpv-item index=other], {"value":"[wpv-user field="ID"]","label":"[wpv-user field="display_name"]"} </wpv-loop> <!-- wpv-loop-end --> [/wpv-items-found] [wpv-layout-end]
Add code to functions.php which will strip extra formatting from the authors View:
add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 ); function prefix_clean_view_output( $out, $id ) { if ( $id == '131' ) { //Please adjust to your Views ID $start = strpos( $out, '<!-- wpv-loop-start -->' ); if ( $start !== false && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false ) { $start = $start + strlen( '<!-- wpv-loop-start -->' ); $out = substr( $out , $start ); $end = strrpos( $out, '<!-- wpv-loop-end -->' ); $out = substr( $out, 0, $end ); } else { $start = strpos( $out, '>' ); if ( $start !== false) { $out = substr( $out, $start + 1 ); $end = strpos( $out, '<' ); $out = trim(substr( $out, 0, $end )); } } } return $out; }
Create a CRED generic field and place the authors View in the options attribute:
[cred_generic_field field='change_post_author' type='select' class='' urlparam=''] { "required":0, "validate_format":0, "default":[], "options":[ [wpv-view name="All Authors"] ] } [/cred_generic_field]
Add cred_save_data hook to functions.php:
add_action('cred_save_data', 'my_save_data_action',10,2); function my_save_data_action($post_id, $form_data) { // if a specific form if ($form_data['id']==131) { $my_post = array( 'ID' => $post_id, 'post_author' => $_POST['change_post_author'] ); // Update the post in the database wp_update_post( $my_post ); } }
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 2 replies, has 2 voices.
Last updated by 7 years, 3 months ago.
Assisted by: Christian Cox.