0

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.
  • Possible duplicate of next and previous post link ordered alphabetically – Nathan Powell Jun 25 at 19:52
  • That is not what I am looking for. I simply want the "Next >>" and "<<Previous" buttons to get the next custom post based on the "name" of the "custom post type" sorted alphabetically. – Vincent Gledhill Jun 25 at 21:29
  • That is the solution. It may seem like something simple, which it sort of is, but the natural WP_Query is going to order posts by post_date. You are wanting it ordered Alphabetically by post_title from what I gather. That requires the custom query. – Nathan Powell Jun 25 at 21:51
  • Did you try the code? I am pretty sure this is the solution. – Nathan Powell Jun 25 at 21:59
  • No it did not. I tried the code, here as follows and the next and previous buttons are still sorted by date order. – Vincent Gledhill Jun 26 at 6:45

Your Answer

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

Browse other questions tagged or ask your own question.