When generating a CRED form you have the option to create/edit Post Title and Featured Image only. I want to edit the Post Menu order.
Please can you advise how this can be achieved?
Many thanks.
Tony
Hi Tony,
Thank you for contacting us and I'll be happy to assist.
To add or update the post's "Menu Order", through a CRED form, you can use "cred_save_data" hook ( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data ), with the "wp_update_post" function ( ref: https://developer.wordpress.org/reference/functions/wp_update_post/ ).
You'll first add a generic field for the menu order value in the form:
<div class="form-group">
<label>Menu Order</label>
[cred_generic_field type='integer' field='menu-oder']
{
"required":0,
"validate_format":1,
"default":""
}
[/cred_generic_field]
</div>
Next, you can add the following code, in the active theme's "functions.php" file:
add_action('cred_save_data', 'my_menu_order_action',10,2);
function my_menu_order_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==123)
{
if (isset($_POST['menu-oder']))
{
$my_post = array(
'ID' => $post_id,
'menu_order' => $_POST['menu-oder'],
);
// Update the post into the database
wp_update_post( $my_post );
}
}
}
Note: Please replace "123" with the actual CRED form's ID, that you'd like to target for this action.
I hope this helps.
regards,
Waqar
Hi Waqar
My issue is resolved now. Thank you!
It all worked fine. For future reference I used the above solution on an EDIT form and changed the line:
"default":""
to
"default":"[wpv-post-menu-order]"
Now I can use this field as a secondary sort in a View and allow my users to maintain the menu-order values.
Many thanks
Tony