0

EDIT: See comments below. changing the question to reflect this new info

From what I understand from the codex, save_post is meant to always fire after the post is saved (and when creating a new post). However, I am experiencing some odd behavior when using Bulk Edit. Using the code below, my sync function runs when using Quick Edit and Bulk Edit just fine, with the exception of syncing Post Format when using Bulk Edit only.

Just to be clear the only things I am syncing that are available with Bulk Edit are author, post format, categories, tags and post status. The all are updating in the WordPress DB fine, but for some reason only author, tags, and categories are syncing to external DB using my sync method. Post Format wont sync, but if I run the Bulk Edit action again, even without changing anything, the syncing does occur, which originally led me to think that the save_post function was firing before the post was saved causing the old data to sync, but that can't be if the rest are working (NOTE: I discovered that is actually is the case, except only for post_format and setting sticky post (which I don't care about)). Here is my code located in my main plugin file:

        require( plugin_admin_dir . 'plugin-sync.php' );
        add_action( 'save_post', 'savethepost', 2000);
        function savethepost($post_id) {
            if (get_post_meta($post_id, '_externaldb_post_donotsync', true) == 1) {
                return;
            } else {
                if (get_post_status( $post_id ) != 'publish') {
                    plugin_sync::externaldb_delete($post_id);
                } else {
                    plugin_sync::externaldb_sync($post_id);
                } // end if
            } // end if
        }

Since I originally posted this I discovered that set_post_format occures after wp_update_post when using Bulk Edit. How can I create a work around to get the new post format when running my sync function? Is there another hook I can use? Anyway, I hope that this is enough information. Thanks for the help.

2
  • Summary: Individual Post Edit = Everything sync fine. Quick Edit = Everything syncs fine. Bulk Edit = everything but Post Format is syncing. Not having a problem the post status functions either.
    – T Andrew
    Commented Nov 17, 2015 at 15:10
  • So after some digging around in the core files, I discovered that set_post_format (admin includes, post.php line 519) is called after wp_update_post (admin includes, post.php line 509) (which runs save_post). Thus, when I hook in my sync function using save_post, the old post_format is still in place when the sync runs. Any suggestions on a work around for this? I need to include the new post format when I run my sync function.
    – T Andrew
    Commented Nov 20, 2015 at 2:50

1 Answer 1

0

I was able to solve my problem by calling set_post_format in my save_post callback and making sure to check if its set and not equal to the "no change" option. Not sure if this is the best way to go, but it works.

require( plugin_admin_dir . 'plugin-sync.php' );
    add_action( 'save_post', 'savethepost', 2000);
    function savethepost($post_id) {
        if (get_post_meta($post_id, '_externaldb_post_donotsync', true) == 1) {
            return;
        } else {
            if (get_post_status( $post_id ) != 'publish') {
                plugin_sync::externaldb_delete($post_id);
            } else {
                if ( isset( $_REQUEST['post_format'] ) && $_REQUEST['post_format'] != -1 ) {
                        set_post_format($post_id, $_REQUEST['post_format']);
                    }
                plugin_sync::externaldb_sync($post_id);
            } // end if
        } // end if
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.