WordPress Tip – Stopping Post Navigation

Every once in a while you may want to display a post but not have the theme show navigation links.  This can happen if you are showing an error message, a private, or perhaps a custom post type.

Your first instinct is probably to modify the theme files. Doing this might be quick, but every time the theme is updated, or every time you change themes you are going to have to be changed the theme files.  So what is the best way.

One technique is to make WordPress think that there aren’t any next or previous posts to link to.  Create a file in your mu-plugins directory with code similar to what is below and the navigation links will never show.  If you wrap the add_filter calls in a conditional that matches your needs you will be all set, no theme mods required!

function stop_post_navigation( $where ) {
   return 'WHERE 1 = 0 ';
}

add_filter( 'get_next_post_where',  'stop_post_navigation', 10, 1 );
add_filter( 'get_previous_post_where',  'stop_post_navigation', 10, 1 );

Leave a Reply

Your email address will not be published. Required fields are marked *