31

Understanding the internals The "sort" order of adjacent (next/prev) posts is not really a sort "order". It's a separate query on each request/page, but it sorts the query by the post_date - or the post parent if you have a hierarchical post as currently displayed object. When you take a look at the internals of next_post_link(), then you see that it's ...


22

Kaiser's answer is awesome and thorough, however just changing the ORDER BY clause isn't enough unless your menu_order matches your chronological order. I can't take credit for this, but I found the following code in this gist: <?php /** * Customize Adjacent Post Link Order */ function wpse73190_gist_adjacent_post_where($sql) { if ( !is_main_query() ...


9

Take a look at get_previous_post() and get_next_post() and you'll see they both use the get_adjacent_post() to find the previous or next post. Let's say you want to fetch the ID of the immediately previous post based on the current post's ID. This is what you'd do: function get_previous_post_id( $post_id ) { // Get a global post reference since ...


7

So in order to do this, I had to filter the SQL queries. I found this snippet that filters post order by title and returns them for next and previous post links. Unfortunately, this doesn't work in a custom post type. So I replaced 'post' with get_post_type($post) to grab the current post type and return it. This then allows this to work in custom post types....


6

No need for functions and filters all you need to do is to use get_adjacent_post instead of next_post_link and prev_post_link, Note that get_adjacent_post is used to get previous and next post, you can read about it here To get previous post and it's title attribute use this $prev_post = get_adjacent_post(false, '', true); if(!empty($prev_post)) { echo '<...


6

I managed to get this working using nothing but WordPress filters, thanks to @Milo's hint. Just note that these are pretty specific to my case but you shouldn't have a problem modifying them for your own use. I am using Advanced Custom Fields with a Date Picker field called date and Prev/Next links only point to events with date fields set to any day before ...


6

Here's a little coding that should implement this for you: <?php $max_length = 5; // set max character length here $next = get_next_post()->ID; $prev = get_previous_post()->ID; if( $prev ) { $title = get_the_title( $prev ); $link = get_the_permalink( $prev ); $post_name = mb_strlen( $title ) > $max_length ? mb_substr( $title, 0, $...


5

I don’t see a way to make the query faster, but we can cache the result. Unfortunately, there is no hook to circumvent next_post_link() and previous_post_link(), so we have to replace those functions with custom functions. The following sample code uses a post meta field to store the result. There might be side effects – I tried to cover most cases, but ...


5

Tried to hook in without success. Might be just a problem of my configuration, but for those who can't make the hook work, here is the simplest solution: <?php $all_posts = new WP_Query(array( 'orderby' => 'menu_order', 'order' => 'ASC', 'posts_per_page' => -1 )); foreach($all_posts->posts as $key => $...


5

As the documentation for next_post_link states, the link parameter is where the actual text of the %link placeholder goes. The format parameter is the whole text, where %link is unpacked from the second link parameter. Solution: next_post_link( '%link', '%title &rarr;' ); Expected output: <a href="...">Title &rarr;</a> Something like ...


5

You can do this by using previous_post_link() and next_post_link(). These functions will create the links for you, and you should be able to get rid of all of the logic you are using for pagination. As you wish to only link to posts in the same category you should use the functions with the following parameters: previous_post_link('&laquo; %link', '%...


5

Reviewing the MySQL string functions, it looks like you could use the SUBSTRING_INDEX() function: ORDER BY SUBSTRING_INDEX( p.post_title, ' ', -1 ) {ASC|DESC} LIMIT 1 to order by the last word of the post title. You could test this method within your orderby filters. Example: Prev/Next CPT - ordered by last word in post title This is an example how ...


5

The correct method of doing this since Wordpress 4.4 is simply the following: add_filter( "get_next_post_where", function($where, $in_same_term, $excluded_terms, $taxonomy, $post){ $where .= " AND p.post_author='".$post->post_author."'"; return $where; }, 10, 5); add_filter( "get_previous_post_where", function($where, $in_same_term, $excluded_terms, ...


4

Here, you can get adjacent post for specific post type with custom sql query & with filter get_{$adjacent}_post_where where default adjacent is previous. Also result depends on $current_post_date & comparison operator $op. function bm_get_adjacent_post( $post_id, $author_id, $previous = 1 ) { global $wpdb; if ( ( ! $post = get_post( $post_id ) ) ...


4

function wpse73190_gist_adjacent_post_sort( $sql ) { $pattern = '/post_date/'; $replacement = 'menu_order'; return preg_replace( $pattern, $replacement, $sql ); } add_filter( 'get_next_post_sort', 'wpse73190_gist_adjacent_post_sort' ); add_filter( 'get_previous_post_sort', 'wpse73190_gist_adjacent_post_sort' );


4

next_posts_link and previous_posts_link (plural) are for archive pagination. For single posts you want next_post_link and previous_post_link (singular).


4

To get next/prev posts, usually all you need is to se to true the parameter that indicates that next/prev post should be in the same taxonomy term in the get next/prev post functions, you don't need to deal with any filter. For example: To get next and prev post objects: // First parameter for these functions indicates // if next/prev post should be in ...


3

The accepted answer works, but probly should have used get_posts() instead of a whole wpquery and loop: if ( ! $post ) { $args = array('posts_per_page'=> 1, 'orderby'=> 'date', 'ignore_sticky_posts' => 1 ); if($previous) $args['order']='DESC'; else $args['order']='ASC'; $...


3

Add following custom functions in functions.php file and instead of calling previous_post_link and next_post_link functions call custom_next_post_link and custom_previous_post_link custom functions respectively. function custom_adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true ) { if ( $previous &&...


3

next_post_link & previous_post_link work off the global $wp_query. You could simply overwrite the main query with $wp_query =& $query, or replace your custom query with the standard 'global' functions. <?php query_posts( array( "post_type" => "page", "page_id" => $post->ID ) ) ?> <?php if ( have_posts() ) : ?> <?php ...


3

i had a pretty similar problem, needed to sort and exclude several posts from prev/next navigation. problem with @cfx's solution was: its not capable for ajax: the is_singular() function returns false, if you load contents via wp-ajax. so it worked on page load, but didn't, when content was changed by ajax. global $post; was helping me out here. here is my ...


3

Naughty boy you, you should never use query_posts. Straight from the codex: Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of ...


3

From the Codex (emphasis mine): In standard usage (within the default, unaltered loop) next_post_link will generate a link to a post that is newer (more recent) than the current post. This is in contrary to the similarly-named previous_posts_link, which will typically link to a page of posts that is older than the current batch. It sounds like ...


3

The next_post_link() and previous_post_link() functions take in_same_term parameter, which is set to FALSE by default. You will find a complete description of the functions here and here. in_same_term (boolean) (optional) Indicates whether previous post must be within the same taxonomy term as the current post. If set to 'true', only posts from ...


2

It sends you to the next/prev post by date of been published. I don't know what gives you these next/prev links and where (single.php, category.php, archive.php or any other custom template file). Without knowing more (some code example) all that I can give to you is this: $page_nr = '&paged='; $page_nr = get_query_var( 'paged' ) ? $page_nr....


2

There are the core functions next/previous_post_link() that accept several arguments. The 3rd is the one that triggers in_same_cat. So if you set that one to TRUE, then you'll only get posts that are in filed under the same category.


2

Try this: function remove_link( $format, $link ) { return false; } add_filter( 'previous_post_link', 'remove_link' ); add_filter( 'next_post_link', 'remove_link' ); It should work if the theme uses next_post_link() and previous_post_link(). Cheers


2

I'm not familiar with Genesis, but I assume you can wrap the output of your function in a check if is_single(): function custom_post_navigation() { if( is_single() ) { // output next/prev links } }


2

OK, it seems that we have two errors here: You should use posts_nav_link() instead of previous_post_link() and next_post_link(). The functions you used point to previous/next posts, not pages. You can refer to the WordPress Codex for more information. You should place posts_nav_link() after endwhile so that it is not repeated for every single post excerpt ...


Only top voted, non community-wiki answers of a minimum length are eligible