/** * Save the new order when called by ajax post */ function ajax_save_ht_kb_article_order() { global $wpdb; try { //check security check_ajax_referer('ht-kb-article-ordering-ajax-nonce', 'security'); //get the new order $items = $_POST['items']; foreach ($items as $key => $item) { $article_id = (int) $item['articleID']; $term_id = (int) $item['termID']; $order = (int) $item['order']; hkb_set_custom_article_order($article_id, $term_id, $order); } //return success message $response_text = __('Article Order updated sucessfully', 'ht-knowledge-base'); $response = array('state' => 'success', 'message' => $response_text); } catch (Exception $e) { //return failure message $response_text = __('Article Order cannot be updated', 'ht-knowledge-base'); $response = array('state' => 'failure', 'message' => $response_text); } echo json_encode($response); die; // this is required to return a proper result }
/** * Set initial custom order meta */ function ht_kb_set_initial_custom_order_meta($post_id) { //get ht_kb_category terms $category_terms = wp_get_post_terms($post_id, 'ht_kb_category'); //loop terms and ensure order is set foreach ($category_terms as $key => $category_term) { $category_term_id = $category_term->term_id; $current_custom_order = hkb_get_custom_article_order($post_id, $category_term_id); //upgrade if empty, set custom order initially to post_id if (empty($current_custom_order)) { hkb_set_custom_article_order($post_id, $category_term_id, $post_id); } } }