• Resolved Pioneer Web Design

    (@swansonphotos)


    We are using the following to set the orderby to the slug (name) and on the archive page this is working fine.

    In functions.php:

    function custom_cpt_order($query) {
    if( is_admin() ) {
    return;
    }
    if(! $query->is_admin) {
    if ($query->get('post_type') == 'our_cpt')
    {
    $query->set('orderby', 'name');
    $query->set('order', 'ASC');
    }
    }
    return $query;
    }
    add_filter('pre_get_posts', 'custom_cpt_order');

    The problem is in the single-cpt page where we want to also have the prev. or next post links use the same orderby of slug (name).

    in single-cpt.php

    if ( is_singular( 'attachment' ) ) {
    // Parent post navigation.
    the_post_navigation( array(
    'prev_text' => _x( '<span class="meta-nav">Published in</span><span class="post-title">%title</span>', 'Parent post link', 'twentysixteen' ),
    ) );
    } elseif ( is_singular( 'our_cpt' ) ) {
    // Previous/next post navigation.
    the_post_navigation( array(
    'next_text' => '<span class="meta-nav" aria-hidden="true">' . __( 'Next CPT in List', 'twentysixteen' ) . '</span> ' .
    '<span class="screen-reader-text">' . __( 'Next post:', 'twentysixteen' ) . '</span> ' .
    '<span class="post-title">%title</span>',
    'prev_text' => '<span class="meta-nav" aria-hidden="true">' . __( 'Previous CPT in List', 'twentysixteen' ) . '</span> ' .
    '<span class="screen-reader-text">' . __( 'Previous post:', 'twentysixteen' ) . '</span> ' .
    '<span class="post-title">%title</span>',
    ) );
    }

    The next prev posts are using the default of DATE and DESC.

    I have reviewed the docs, researched several well known sites and forums and find little if anything on the right way to do this.

    Working examples (code) greatly appreciated. Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    You would hook the filters “get_next_post_sort” and “get_previous_post_sort”. Your callback function is passed "ORDER BY p.post_date [DESC|ASC] LIMIT 1". Alter as necessary to achieve the ordering you want, then return the modified string.

    It would be useful to see the entire query string if you are not sorting by a post value. Unfortunately, there is no easy way to see the entire string. Temporarily hack the core code to output the string contained in $query at line 1833 in /wp-includes/link-template.php. Once you’ve worked out what to do, restore the original file.

    Thread Starter Pioneer Web Design

    (@swansonphotos)

    Please document for the world the allowed orderby args that function. Seems it’s quite limited.

    Moderator bcworkz

    (@bcworkz)

    It’s already documented here. It’s common for higher level functions to be more limited in their scope. For those long tail needs, one can use the “posts_orderby” filter to specify any ordering allowed by SQL.

    That applies to WP_Query. the_post_navigation() doesn’t support alternative ordering at all, except through the filter “get_{$adjacent}_post_sort”.

    Thread Starter Pioneer Web Design

    (@swansonphotos)

    Due to actual lack of support for this issue, I found my own workaround.

    Thanks for trying.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘CPT nav links’ is closed to new replies.