Skip to content

Commit

Permalink
🐛 Fix issue CPO creating new post
Browse files Browse the repository at this point in the history
  • Loading branch information
daomapsieucap committed Sep 16, 2022
1 parent d2e2192 commit ac6b24a
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions includes/cpo.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,25 @@ public function fiad_cpo_insert($post_id, $post, $update){
global $wpdb;
$order_start = 0;

$pre_posts_args = array(
$wpdb->update($wpdb->posts, array('menu_order' => $order_start), array('ID' => intval($post_id)));

$update_posts_args = array(
'post_type' => $post->post_type,
'posts_per_page' => 1,
'posts_per_page' => - 1,
'post_status' => 'publish',
'orderby' => 'menu_order',
'order' => 'DESC',
'order' => 'ASC',
'post__not_in' => array($post_id),
'suppress_filters' => false,
'fields' => 'ids'
);
$pre_posts_query = new WP_Query($pre_posts_args);
if($pre_posts_query->have_posts()){
$order_start_id = $pre_posts_query->posts[0];
$order_start_post = get_post($order_start_id);
$order_start = $order_start_post->menu_order;
$update_posts_query = new WP_Query($update_posts_args);
if($update_posts_query->have_posts()){
foreach($update_posts_query->posts as $index => $id){
$wpdb->update($wpdb->posts, array('menu_order' => intval($index + 1)), array('ID' => intval($id)));
$order_start ++;
}
}

$wpdb->update($wpdb->posts, array('menu_order' => $order_start + 1), array('ID' => intval($post_id)));
}
}

Expand Down

0 comments on commit ac6b24a

Please sign in to comment.