Get Links to Next and Previous Posts in WordPress Post Editor

Print Page

This code snippet will add navigation links to next and previous posts in the WordPress Post Editor page. Navigation links will appear for all posts, pages, and custom post types.

wordpress admin post navigation

Drop this code to your active theme’s functions.php file:

<?php
/**
 * Adds previous/next links to post edition window
 *
 * @return string
 */
function add_navigation_edit_posts() {
        if ( isset( $_GET['post'] ) and isset( $_GET['action'] ) and $_GET['action'] == 'edit' ) {
                global $post;

                $args = array(
                        'public'   => true,
                        '_builtin' => false,
                );
                $output   = 'names'; // names or objects, note names is the default
                $operator = 'and'; // 'and' or 'or'

                $post_types = get_post_types( $args, $output, $operator );

                $post_types['post'] = 'post';
                $post_types['page'] = 'page';

                if ( is_object( $post ) and in_array( $post->post_type, $post_types ) ) {
                        foreach ( array( true, false ) as $prev ) {
                                $p = get_adjacent_post( false, '', $prev );

                                if ( ! empty( $p ) ) {
                                        echo '<script>
                                        jQuery(function($) {
                                                $(".wrap h2" )
                                                .append('<a class="add-new-h2" href="' . admin_url( 'post.php?action=edit&post=' . $p->ID ) . '" title="' . __( 'Edit' ) . ' ' . apply_filters( 'the_title', $p->post_title ) . '">' . ( $prev ? '« ' : '' ) . apply_filters( 'the_title', $p->post_title ) . ( ! $prev ? ' »' : '' ) . '</a>' );
                                        });
                                        </script>';
                                }
                        }
                }
        }
}

add_action( 'admin_head', 'add_navigation_edit_posts' );
?>

via Alex Sancho