Context: We created a plugin, that allows posts to be either in web, mobile app or both. This is done via meta key. When shown only in mobile app mode, then its hidden from web. The plugin code, that filters with meta.

Problem: While it is hidden from search, general posts and direct. But it is still visible in the next prev navigations under the posts content. We need to filter the post navigation somehow based on meta key.

I can see where the get_the_post_navigation() function is located and I can see in the theme, where <?php if( get_theme_mod( 'post_navigation', false ) === false ) the_post_navigation(); ?> is located.

Can get_the_post_navigation or post_navigation be filtered using meta values, or only option is to not use the post_navigation completely, and make a custom function to show next and prev. Similar to this answer.

The content-single.php is the initiator for the_post_navigation() as so:

<?php if( get_theme_mod( 'post_navigation', false ) === false ) the_post_navigation(); ?>

the_post_navigation() located inside wp-includes/link-template.php:

function the_post_navigation( $args = array() ) {
    echo get_the_post_navigation( $args );

And the get_the_post_navigation() inside the link-template as well:

function get_the_post_navigation( $args = array() ) {
    // Make sure the nav element has an aria-label attribute: fallback to the screen reader text.
    if ( ! empty( $args['screen_reader_text'] ) && empty( $args['aria_label'] ) ) {
        $args['aria_label'] = $args['screen_reader_text'];

    $args = wp_parse_args(
            'prev_text'          => '%title',
            'next_text'          => '%title',
            'in_same_term'       => false,
            'excluded_terms'     => '',
            'taxonomy'           => 'category',
            'screen_reader_text' => __( 'Post navigation' ),
            'aria_label'         => __( 'Posts' ),

    $navigation = '';

    $previous = get_previous_post_link(
        '<div class="nav-previous">%link</div>',

    $next = get_next_post_link(
        '<div class="nav-next">%link</div>',

    // Only add markup if there's somewhere to navigate to.
    if ( $previous || $next ) {
        $navigation = _navigation_markup( $previous . $next, 'post-navigation', $args['screen_reader_text'], $args['aria_label'] );

    return $navigation;
  • Maybe you want to add to your question how your loop code looks in the page you want to change this pagination? It sounds more like in the query that generates those results you should be doing the meta value query and so this should 'just work' for all paginated results.
    – mozboz
    Aug 1, 2020 at 17:09
  • TBH, since WP is not my favorite thing in the world, I do not fully understand your question. Mostly because, I don't know which loop is where. But its some standard theme and as I understand, the post_navigation comes from built in functions. But I do understand the query idea. And that is exactly what I want to do, but I dont see, where the native next prev navigation gets its data. I would have expected the pre_get_posts override to already take care of this. Aug 1, 2020 at 17:41
  • ah, you have a pre_get_posts hook to set the meta_key and meta_value parameter already? AFAIK that should also take care of the pagination as it should apply to every page. you may want to post how that code looks in case someone spots something wrong with it
    – mozboz
    Aug 1, 2020 at 18:23
  • @mozboz This is the plugin code: pastebin.com/tVD36Ksu And it does seem to work for everything, except for the next prev posts. Aug 1, 2020 at 18:29
  • @mozboz I added further details to the question. Im looking now, that get_previous_post_link() and get_next_post_link() are the sources for the posts. So I should focus on them I think. Aug 1, 2020 at 19:15

1 Answer 1


In your code this in the condition where you decide when to apply the meta query condition:

if($query->is_main_query()) {

Therefore if the conditions inside there are working sometimes and not others, it means that condition is true when it's working and not when it's not.

The solution to this is dependent on all the code running on your site, but first I'd suggest validating this theory by changing this line to the below. Do this on your development environment or temporarily in production if you have to

if(true) {

This means those meta conditions will get applied to every query. Obviously you don't want that, but if your other paginated pages start working as you want that will confirm this is the problem.

If it is the problem, you just need to find another condition that suits what you need better than is_main_query. For example is_home could be what you're looking for

If that's not the problem, then something else more complicated is happening on your other paginated pages that's messing with your queries, or using a different method that's ignoring these parameters.

  • Sorry, no effect with the if(true). Posts are still filtered, but pagination shows the app only post as next. Aug 1, 2020 at 19:08
  • Ah, sorry it didn't work, was worth a go. That's surprising but perhaps someone else will know what's going on, or you might have something super funky happening on other pages that's caused by some other code you have altering the query
    – mozboz
    Aug 1, 2020 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.