Skip Navigation

[Résolu] Select Author when adding a new Custom Post

This support ticket is created Il y a 8 années et 5 mois. There's a good chance that you are reading advice that it now obsolete.

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Marqué : 

This topic contains 2 réponses, has 1 voix.

Last updated by eldredC Il y a 8 années et 5 mois.

Auteur
Publications
#340132
Author Select.PNG

I am trying to: Create a frontend form in CRED.

I have a custom Post called Commission Payments Received.

The logged in user will be an Admin and he will be adding a commission payment received on behalf of another registered user.

I want to be able to select the correct author/registered user to assign the post to.

So how do I display a select box showing the authors of a certain role to then assign the post to?

#340138

I have the following code in the three different places.

The select Box functions correctly and the HTML listed below.

Unfortunately the Author is always set to the admin who is logged in and not the selected author from the select box.

HTML

<select name="my_author_select" data-wpt-type="select" class="wpt-form-select form-select select" id="cred_form_1254_1-select-1-1444877006">
<option data-wpt-name="my_author_select" data-wpt-id="cred_form_1254_1_cred_form_1254_1-select-1-1444877006" data-wpt-type="option" class="wpt-form-option form-option option" value="27">James Taylor</option>
<option data-wpt-name="my_author_select" data-wpt-id="cred_form_1254_1_cred_form_1254_1-select-1-1444877006" data-wpt-type="option" class="wpt-form-option form-option option" value="34">Pieter Double</option>
<option data-wpt-name="my_author_select" data-wpt-id="cred_form_1254_1_cred_form_1254_1-select-1-1444877006" data-wpt-type="option" class="wpt-form-option form-option option" value="33">Jonathan Bidmead</option>
<option data-wpt-name="my_author_select" data-wpt-id="cred_form_1254_1_cred_form_1254_1-select-1-1444877006" data-wpt-type="option" class="wpt-form-option form-option option" value="12">Pieter Double</option>
</select>

View

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
        [wpv-item index=1]
          {"value":"[wpv-user field="ID"]","label":"[wpv-user field="user_firstname"] [wpv-user field="user_lastname"]"}   
        [wpv-item index=other],
          {"value":"[wpv-user field="ID"]","label":"[wpv-user field="user_firstname"] [wpv-user field="user_lastname"]"}
</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

Cred Form

[credform class='cred-form cred-keep-original']

	[cred_field field='form_messages' value='']
	[cred_generic_field field="my_author_select" type="select" class="" urlparam=""]
		{
			"required":0,
			"validate_format":0,
			"persist":1,
			"default":[],
			"options":[ [wpv-view  name="List Authors"] ]
		}
	[/cred_generic_field]
	<div class="cred-group cred-group-Commission-Payments-Received">
		<div class="cred-header"><h3>Commission Payments Received</h3></div>
		<div class="cred-field cred-field-commission-amount">
			<label class="cred-label">
Commission Amount
</label>
			[cred_field field='commission-amount' post='comm-acc-payment' value='' urlparam='']
		</div>
		<div class="cred-field cred-field-policy-commission-received-from">
			<label class="cred-label">
Policy Commission Received From
</label>
			[cred_field field='policy-commission-received-from' post='comm-acc-payment' value='' urlparam='']
		</div>
	</div>

	[cred_field field='form_submit' value='Submit' urlparam='']

[/credform]

Functions File

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']==1254)
   
{
$my_post = array(
      'ID'           => $post_id,
      'post_author' => $form_data['my_author_select']
  );
  
// Update the post into the database
  wp_update_post( $my_post );
}
}
#340144

I fixed it in the end

I had to change

'post_author' => $form_data['my_author_select']

as it was trying to insert the 'label' rather than the 'id' into the post.
to

'post_author' => $_POST['my_author_select']
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.