Custom post type, next and previous links by page title

I have a site that I am converting to wordpress and have a problem with the next post and previous post, because the default value is to sort the posts by date order. However, I want to sort the next "custom post" which is an airfield card, by "name".

When I put all of the airfields into the site, I started off at the beginning, and worked my way along, therefore the first couple of pages of entry's work fine. However, I used a tool to drag in the rest of the information from another database. So now, when I am on airfield "Rufforth", I get the next airfield as "Eddsfield" not "Rougham" which is the next airfield alphabetically by name. My current theme has the following code, please could someone explain how I need to change it to do what I require. I have searched the codex, and various articles and questions, all to no avail.

?php
    while ( have_posts() ) :
        the_post();
        the_content();
            // If comments are open or we have at least one comment, load up the comment template
        if ( comments_open() || '0' != get_comments_number() ) :
            comments_template();
            endif;


        /********** the new inserted sort function *********/

        function filter_next_post_sort($sort) {
          if (get_post_type($post) == 'airfield_card') {
            $sort = "ORDER BY p.post_title ASC LIMIT 1";
          } else {
            $sort = "ORDER BY p.post_date ASC LIMIT 1";
          }
          return $sort;
        }


        /************* end of sort function ***********/


        the_post_navigation(
            array(
                'next_text' = 'span class="post-title"%title i class="fa fa-chevron-right"/i/span',
                'prev_text' = 'i class="fa fa-chevron-left"/i span class="post-title"%title/span',
            )
        );

    endwhile; // end of the loop.

Topic next pagination custom-post-types Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.