WordPress get next or previous ID for a custom post type in a menu reorder (Drag position)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
wp_reset_query(); // get_posts in same custom taxonomy $postlist_args = array( 'posts_per_page' => -1, 'orderby' => 'menu_order', 'post_type' => 'etudes_de_cas' ); $postlist = get_posts( $postlist_args ); // get ids of posts retrieved from get_posts $ids = array(); foreach ($postlist as $thepost) { $ids[] = $thepost->ID; } // get and echo previous and next post in the same taxonomy $thisindex = array_search($post->ID, $ids); $previd = $ids[$thisindex-1]; $nextid = $ids[$thisindex+1]; if ( empty($previd) ) { $lastPos = count($ids); $previd = $ids[$lastPos-1]; } if ( empty($nextid) ) { $nextid = $ids[0]; } |