Skip Navigation

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

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

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 support ticket is created hace 6 meses, 3 semanas. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Este tema contiene 1 respuesta, tiene 2 mensajes.

Última actualización por Christopher Amirian hace 6 meses, 3 semanas.

Asistido por: Christopher Amirian.

Autor
Mensajes
#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

Idiomas: Inglés (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.