I'm trying to limit previous/next navigation to posts that have the same meta_value on a certain meta_key. I know this was asked before, but I couldn't pick a working answer. And all that I could come up with by myself is this:

$keyword = get_post_meta($post->ID, 'keyword', true);
$prevPost = get_previous_post(); 
$args = array('meta_key' => 'keyword', 'meta_value' => $keyword, 'posts_per_page' => 1, 'include' => $prevPost->ID); 
$prevPost = get_posts($args); foreach ($prevPost as $post) {

...but it seems to only work when two posts are next to each other chronologically as well, else it will display nothing. I suppose functions should be run in a different order, but I have no idea how to go about it.


The plugin "Ambrosite Next/Previous Post Link Plus" https://wordpress.org/plugins/ambrosite-nextprevious-post-link-plus/ ..seems to do the job well

To further style the results, one could even use it like this

$prev = previous_post_link_plus( array('return' => 'id') );
query_posts('p='.$prev); while (have_posts()) : the_post();
[...your stuff here...]
endwhile; wp_reset_query();
$next = next_post_link_plus( array('return' => 'id') );
query_posts('p='.$next); while (have_posts()) : the_post();
[...your stuff here...]
endwhile; wp_reset_query();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy