Skip Navigation

[Resolved] Editing the Menu Order Value using a Toolset Form

This thread is resolved. Here is a description of the problem and solution.

Problem:

I want to use a Toolset Form to edit the Menu Order value of posts, but I can't see a way to do this through the standard Toolset Form functionalities.

Solution:

Add a generic input to the form with the slug name the-order. Then, add custom code to the Toolset custom code section to update the Menu Order using cred_save_data action.

Sample code to get started:

add_action('cred_save_data', 'update_post_order', 10, 2);
function update_post_order($post_id, $form_data) {
    // Check if the correct form is being submitted
    if ($form_data['id'] == [YOUR_FORM_ID]) { // Replace [YOUR_FORM_ID] with the actual ID of your form
        if (isset($_POST['the-order'])) {
            $new_order = intval($_POST['the-order']); // Ensure the input is treated as an integer
 
            // Update the post order
            $args = array(
                'ID'         => $post_id,
                'menu_order' => $new_order
            );
            wp_update_post($args);
        }
    }
}

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code.

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.

This topic contains 1 reply, has 2 voices.

Last updated by Christopher Amirian 6 months, 2 weeks ago.

Assisted by: Christopher Amirian.

Author
Posts
#2697091

I have a View that's using Menu Order as the secondary sorting and it works great.

I can edit the Menu Order value using the standard WP backend post edit functionality.

I would like to use a Toolset Form to edit the Menu Order value but I cannot see a way.

#2697278

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

This will need custom coding to be implemented. I did not test it on my installation but I have an idea that you can use as a starting point and play with it to find the solution.

- Add a generic input to the form with the slug name of the-order
- Add the code below to the Toolset custom code section. For more information:
https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code

add_action('cred_save_data', 'update_post_order', 10, 2);
function update_post_order($post_id, $form_data) {
    // Check if the correct form is being submitted
    if ($form_data['id'] == [YOUR_FORM_ID]) { // Replace [YOUR_FORM_ID] with the actual ID of your form
        if (isset($_POST['the-order'])) {
            $new_order = intval($_POST['the-order']); // Ensure the input is treated as an integer

            // Update the post order
            $args = array(
                'ID'         => $post_id,
                'menu_order' => $new_order
            );
            wp_update_post($args);
        }
    }
}

- Replace [YOUR_FORM_ID] with the ID of the form you're working with.

The code does the steps below:

Retrieves the user input for the post order from the form. Assuming the slug of the input field is the-order, this value can be accessed through the $_POST superglobal.

Uses the wp_update_post function to update the menu_order attribute of the post based on the user input.

I think that would be a good starting point.

Thanks.

#2698372

That did it for me!
The only problem I encountered was when editing an existing form it wasn't pulling in the current value.
That was solved by including the wpv-post-menu-order shortcode into the Default field value for that field.
Thanks a bunch.