10up / simple-page-ordering

Order your pages and other hierarchical post types with simple drag and drop right from the standard page list.
https://wordpress.org/plugins/simple-page-ordering/
GNU General Public License v2.0
149 stars 23 forks source link

Pages that have associated meta-post data loose said data when they are moved #213

Closed sunaj closed 3 months ago

sunaj commented 3 months ago

Describe the bug

I use a standard function.. add_action('save_post', 'save_custom_fields', 10, 2); function save_custom_fields($page_id, $page_instance) { if ($page_instance->post_type != 'page') { return; } update_post_meta($page_id, 'custom_glyph', $_POST['custom_glyph']); } To save a glyph in the meta data of each page. This works as intended. Now: as soon as a Page is moved this data is discarded. I verified this by checking the "wp_postmeta" table: data there, move Page, data gone.

Steps to Reproduce

  1. Save any Page with some associated meta data
  2. Move a Page
  3. Now meta data are gone

Screenshots, screen recording, code snippet

No response

Environment information

No response

WordPress information

No response

Code of Conduct

sunaj commented 3 months ago

OK, a bit of further investigation on my part proved that the issue is "wp_update_post(...)" inside "class-simple-page-ordering.php" stripping the meta data, if I insert a "get_post_meta(...)" prior to the the wp_update_post and an "update_post_meta(...)" after it to manually retrieve and re-insert the meta data needed this fixes the issue.

dkotter commented 3 months ago

Is this code snippet you shared the exact code you're currently running:

add_action( 'save_post', 'save_custom_fields', 10, 2 );
function save_custom_fields( $page_id, $page_instance ) {
    if ( $page_instance->post_type != 'page' ) {
        return;
    }

    update_post_meta( $page_id, 'custom_glyph', $_POST['custom_glyph'] );
}

If so, I think the issue here is the $_POST['custom_glyph'] value won't exist when the re-ordering happens, which will then update that meta field to be empty. As you found, this plugin runs wp_update_post in a few areas in order to save the correct menu_order and post_parent. When that runs, eventually the save_post hook will fire and your code above will run. But in that context, there isn't a $_POST['custom_glyph'] value ever sent and thus the custom_glyph meta field will be updated to an empty value.

If you make the following update to your code, it should fix things I believe:

add_action( 'save_post', 'save_custom_fields', 10, 2 );
function save_custom_fields( $page_id, $page_instance ) {
    // Only update the custom field if we're saving a Page and our custom value exists.
    if ( $page_instance->post_type != 'page' && ! isset( $_POST['custom_glyph'] ) ) {
        return;
    }

    update_post_meta( $page_id, 'custom_glyph', $_POST['custom_glyph'] );
}
peterwilsoncc commented 3 months ago

@sunaj Are you able to test the code suggested in the comment above and let the team know if you are still having problems?

sunaj commented 3 months ago

@peterwilsoncc Sorry for delay, weekend kicked in! Yes, I patched my code as suggested and things started working according to plan! This can be considered closed - a non-bug with fix on user-end implemented. Thanks for prompt help 👍

peterwilsoncc commented 3 months ago

Thanks for getting back to us, @sunaj, it's really helpful.

I'll modify the closed status to unplanned (not reproducible).