Skip to content

Instantly share code, notes, and snippets.

@joshuafredrickson
Created February 1, 2017 23:53
Show Gist options
  • Save joshuafredrickson/a5cff894257eb76fb842bf256303db12 to your computer and use it in GitHub Desktop.
Save joshuafredrickson/a5cff894257eb76fb842bf256303db12 to your computer and use it in GitHub Desktop.
hijiriworld/intuitive-custom-post-order -- Scrap existing menu_order values after reordering.
// Fixes duplicate items showing up if the same menu_order is shared between two items across a page break.
// Replace update_menu_order() with the code below.
function update_menu_order()
{
global $wpdb;
parse_str( $_POST['order'], $data );
if ( !is_array( $data ) ) return false;
// get objects per now page
$id_arr = array();
foreach( $data as $key => $values ) {
foreach( $values as $position => $id ) {
$id_arr[] = $id;
}
}
// get menu_order of objects per now page
$menu_order_arr = array();
foreach( $id_arr as $key => $id ) {
$results = $wpdb->get_results( "SELECT menu_order FROM $wpdb->posts WHERE ID = ".intval( $id ) );
foreach( $results as $result ) {
$menu_order_arr[] = $result->menu_order;
}
}
// maintains key association = no
sort( $menu_order_arr );
// Orangepineapple.com -- Scrap old $menu_order_arr; use the min value as a starting point for new menu_order.
$first_post = min($menu_order_arr);
foreach( $data as $key => $values ) {
foreach( $values as $position => $id ) {
// Orangepineapple.com -- Scrap old $menu_order_arr; use the min value as a starting point for new menu_order.
// $wpdb->update( $wpdb->posts, array( 'menu_order' => $menu_order_arr[$position] ), array( 'ID' => intval( $id ) ) );
$post_order = $first_post + $position;
$wpdb->update( $wpdb->posts, array( 'menu_order' => $post_order ), array( 'ID' => intval( $id ) ) );
}
}
}
@joshuafredrickson
Copy link
Author

Fixes duplicate items showing up if the same menu_order is shared between two items across a queried page break.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment