0

I have a code that I'm working on to enable a user to manually order posts in a custom post type, a lot of the code I basically copy-pasted from an already-existing plugin that has this functionality, then butchered down to functionality that I actually needed & tried my best to rename variables & identifiers to my needs, but I can't quite get it to save.

I have the drag-and-drop reorder functionality that I need, but when I click the save button, it just reloads the page without saving the order or anything. What am I missing?

function get_sops_in_order( $limit = null, $group = false ) {

if ( is_null( $limit ) ) {
    $limit = get_option( Options::DISPLAY_LIMIT );
}

$args = array(
    'post_type'      => 'sops',
    'posts_per_page' =>  empty( $limit ) || strtolower( $limit == 'all' ) ? -1 : $limit,
    'meta_key'       => 'sops_order',
    'orderby'        => 'meta_value_num',
    'order'          => 'ASC',
);

if( !empty( $group ) ) {
    $group = (array) $group;

    if ( is_int( $group[0] ) ) {
        $args['tax_query'][] = array(
            'taxonomy'  => 'sops_position',
            'field'     => 'term_id',
            'terms'     => array_map(function ($g) { return absint($g); }, $group)
        );

    } else {
        $args['tax_query'][] = array(
            'taxonomy'  => 'sops_position',
            'field'     => 'slug',
            'terms'     => $group
        );
    }
}

return new \WP_Query( $args ); } 
function save_sops_order() {

    if( isset( $_POST['sops_order_nonce'] ) &&
        wp_verify_nonce( $_POST['sops_order_nonce'], 'save_sops_order' ) ) {

        $ids = array();

        // Extract the URL encoded IDs
        parse_str( $_POST['sops_order'], $ids );


        for( $ctr = 1; $ctr < count( $ids['sops'] ) + 1; $ctr++ ) {
            update_post_meta( $ids['sops'][ $ctr - 1 ], 'sops_order', $ctr );
        }


        add_settings_error( 'sops-reorder', 'save-order', __( 'Order Successfully updated', 'ots' ), 'updated' );

    }
}
function sops_order_sort_callback() { 
?>
 <?php $sops = get_sops_in_order( false ); ?>
<div class="wrap">
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script> <!-- Grabbing newer version of jquery for .sortable() function -->
    <script>
        jQuery(document).ready(function ($) {

var sops = $('#od-sop-order').sortable({
    update: update_order
});


function update_order() {
    $('[name="sops_order"]').val(sops.sortable('serialize'));
}

update_order();
});


    <?php settings_errors( 'sops-reorder' ); ?>

    <h1>Drag & Drop to Reorder SOPs</h1>

    <?php if( $sops->have_posts() ) : ?>

        <form method="post">

            <ul id="od-sop-order">

                <?php foreach( $sops->posts as $sop ) : ?>

                    <li id="sops_<?php esc_attr_e( $sop->ID ); ?>" class="ots-sop">

                        <div class="inner">
                            <div class="sop-info">
                                <a class="edit" href="<?php echo esc_url( get_edit_post_link( $sop->ID ) ); ?>">
                            <span class="dashicons dashicons-edit"></span>
                        </a>
                        <h2 class="name"><?php echo esc_html_e( $sop->post_title ); ?></h2>
                                <p class="description job-title"><?php esc_html_e( get_post_meta( $sop->ID, 'sops_title', true ) ); ?></p>
                            </div>

                        </div>

                    </li>

                <?php endforeach; ?>

            </ul>
            <div class="clear"></div>
            <input type="hidden" name="sops_order" />
            <input type="hidden" name="action" value="ots_save_sops_order" />

            <?php wp_nonce_field( 'save_sops_order', 'sops_order_nonce' ); ?>

            <?php submit_button( __( 'Save Order', '' ) ); ?>

        </form>

    <?php else : ?>

        <h3><?php _e( 'You haven\'t added any team sops yet. <a href="edit.php?post_type=sops">Add a new sop</a>', 'ots' ); ?></h3>

    <?php endif; ?>

</div>

Try to ignore the awful readability - it's not great on my end to begin with, plus stackexchange butchers it further when copy-pasting the code over

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.