Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to create a way to change the order of posts using a meta_key / meta_value that is updated using jQuery ui-sortable.

I'm already using menu_order in the same way but need to create a way to preview the new order before saving the new menu_order values.

My Ajax function that is not working:

function wnd_preview_item_order() {
    global $wpdb;
    $order = explode(',', $_POST['order']);
    $counter = 0;
    foreach ($order as $item_id) {
        $wpdb->update($wpdb->postmeta, array( 'meta_key' => '_wnd_pre_order', 'meta_value' => $counter ), array( 'ID' => $item_id) );
        $counter++;
    }
    die(1);
}
add_action('wp_ajax_item_sort', 'wnd_preview_item_order');
add_action('wp_ajax_nopriv_item_sort', 'wnd_preview_item_order');

I'm able to update the menu_order with no problems using a similar Ajax function but I'm stuck on changing the meta_key value.

Sortable js:

Update: I made some adjustments to the js to save the order on click.

    jQuery(document).ready(function($) {
        $( "#<?php echo $catID; ?>-sortable" ).sortable({
        cursor:'move',
        items:'.lineitem',
        placeholder:'sortable-placeholder',
        tolerance: 'pointer',
        forcePlaceholderSize: true,
        helper: 'clone',
        opacity: 0.65
    });
    $("#<?php echo $catID; ?>-save-order").bind( "click", function() {
       $.post( ajaxurl, { action:'item_sort', order: $( "#<?php echo $catID; ?>-sortable" ).sortable('toArray').toString() },
       function() {
       $("#<?php echo $catID; ?>-ajax-response").html('<div class="message updated fade"><p>Post Order Updated</p></div>');
       $("#<?php echo $catID; ?>-ajax-response div").delay(3000).hide("slow");
    });
       });

});

My Query to display the posts using the meta_value orderby:

$exclude =  $GLOBALS['current_id'];
            if ($count >= 4 ) : $ppp = 4; else : $ppp = 7; endif;
            $args = array(
            'cat' => $category->term_id,
            'meta_key'  => '_wnd_pre_order',
            'orderby'    => 'meta_value_num',
            'order'      => 'ASC',
            'post__not_in' => array($exclude),
            'posts_per_page' => $ppp
        );
        $cat_query = new WP_Query( $args );

Update:

This is the Ajax function for changing the menu_order which does work:

function wnd_save_item_order() {
     global $wpdb;
    $order = explode(',', $_POST['order']);
    $counter = 0;
    foreach ($order as $item_id) {
        $wpdb->update($wpdb->posts, array( 'menu_order' => $counter ), array( 'ID' => $item_id) );
        $counter++;
    }
    die(1);
}
share|improve this question
    
Why don't you just update the menu_order column in the wp_posts table? This could save you considerable work as you can just orderby menu_order. –  Brian Fegter Sep 19 '11 at 1:56
    
Well what's the error (the value of e) ? Just so you know, you're requesting a json response, but the wnd_preview_item_order function is not sending any json encoded data. –  onetrickpony Sep 19 '11 at 2:10
    
@BrianFegter I am using menu_order but my client wants the ability to preview the page with the new order before saving. –  Chris_O Sep 19 '11 at 2:56
    
@OneTrickPony It was alerting success. –  Chris_O Sep 19 '11 at 3:01
1  
Is the question still relevant or can be closed? –  Rarst Oct 8 '12 at 20:09
show 3 more comments

closed as too localized by toscho Dec 18 '12 at 23:52

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

Browse other questions tagged or ask your own question.