Advertisement
Guest User

Untitled

a guest
Jan 25th, 2013
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. /**
  2. * Calls the class on the post edit screen
  3. */
  4. function Call_Next_Previous_Post_Edit_Class() {
  5. return new Next_Previous_Post_Edit_Class();
  6. }
  7.  
  8. if ( is_admin() )
  9. add_action( 'load-post.php', 'Call_Next_Previous_Post_Edit_Class' );
  10.  
  11. /**
  12. * The Class
  13. */
  14. class Next_Previous_Post_Edit_Class {
  15.  
  16. const LANG = 'post-edit-navigation';
  17. public $post_types = array('post', 'page');
  18.  
  19. public function __construct() {
  20. add_action( 'add_meta_boxes', array( &$this, 'add_some_meta_box' ) );
  21. }
  22.  
  23. /**
  24. * Adds the meta box container
  25. */
  26. public function add_some_meta_box() {
  27. $args=array(
  28. 'public' => true,
  29. '_builtin' => false
  30. );
  31. $output = 'names'; // names or objects, note names is the default
  32. $operator = 'and'; // 'and' or 'or'
  33. $this->post_types = array_merge($this->post_types , (array) get_post_types($args, 'names', 'and'));
  34. foreach($this->post_types as $post_type) {
  35. add_meta_box(
  36. 'some_meta_box_name'
  37. ,__( 'Edit Previous and Next '.$post_type, self::LANG )
  38. ,array( &$this, 'render_meta_box_content' )
  39. , $post_type
  40. ,'advanced'
  41. ,'high'
  42. );
  43. }
  44. }
  45.  
  46.  
  47. /**
  48. * Render Meta Box content
  49. */
  50. public function render_meta_box_content($post) {
  51.  
  52. global $wpdb;
  53. $query = "
  54. SELECT DISTINCT * FROM $wpdb->posts
  55. WHERE $wpdb->posts.post_type = '$post->post_type'
  56. AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'future' OR $wpdb->posts.post_status = 'draft' OR $wpdb->posts.post_status = 'pending')
  57. AND $wpdb->posts.post_date
  58. ";
  59.  
  60. $previous = $wpdb->get_results($query."<= '$post->post_date' ORDER BY post_date DESC LIMIT 1,5");
  61. $next = $wpdb->get_results($query.">= '$post->post_date' ORDER BY $wpdb->posts.post_date ASC LIMIT 1,5");
  62.  
  63. if($previous || $next){
  64.  
  65. echo '<p>';
  66. if($previous) {
  67. $previous = array_reverse($previous);
  68. foreach ($previous as $prev_post) {
  69. $title = (isset($prev_post->post_title) && $prev_post->post_title) ? $prev_post->post_title : 'No Title';
  70. echo 'Edit Previous: <a href="' . get_edit_post_link($prev_post->ID) . '">' . $title . '</a><br/>';
  71. }
  72. }
  73.  
  74. // current post
  75. echo '<span style="color: red;">Current Post: ' . $post->post_title . '</span><br/>';
  76.  
  77. if($next) {
  78. foreach ($next as $next_post) {
  79. $title = (isset($next_post->post_title) && $next_post->post_title) ? $next_post->post_title : 'No Title';
  80. echo 'Edit Next: <a href="' . get_edit_post_link($next_post->ID) . '">' . $title . '</a><br/>';
  81. }
  82. }
  83. echo '</p>';
  84. }
  85. }
  86.  
  87. } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement