0

In order to apply chronological order with posts and alphabetical order with a certain Custom Post Type, let's say, "animals", displayed through a single-animals.php template, I found this snippet that works fine:


/*
*
* Sort Next/Previous Post Links Alphabetically for Custom Post Type "animals" and Chronologically for the Rest Posts
*
*/
    
function filter_next_post_sort($sort) {
    if (get_post_type($post) == 'animals') {
        $sort = "ORDER BY p.post_title ASC LIMIT 1";
    }
    
    else{
        $sort = "OR DER BY p.post_date ASC LIMIT 1";
    }
return $sort;
}
    

function filter_next_post_where($where) {
    global $post, $wpdb;
    if (get_post_type($post) == 'animals') {
        return $wpdb->prepare("WHERE p.post_title > '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'",$post->post_title);
    }
    else{
        return $wpdb->prepare( "WHERE p.post_date > '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'", $post->post_date, $post->post_type );
   }
}
    

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

function filter_previous_post_where($where) {
     global $post, $wpdb;
     if (get_post_type($post) == 'animals') {
         return $wpdb->prepare("WHERE p.post_title < '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'",$post->post_title);
     }
     else{
         return $wpdb->prepare( "WHERE p.post_date < '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'", $post->post_date, $post->post_type );
     }
}

add_filter('get_next_post_sort', 'filter_next_post_sort');
add_filter('get_next_post_where', 'filter_next_post_where');
add_filter('get_previous_post_sort', 'filter_previous_post_sort');
add_filter('get_previous_post_where', 'filter_previous_post_where');


 Now, when using this snippet in a multilingual site with WP 5.1.1 and Polylang plugin, what I get is the next/previous links for all the items of the "animals" custom post type, no matter their language or the current post language. 
 So, when using, let' say, English, German and Spanish, as site languages, the result in single-animals.php template is an ordered navigation sequence of previus/next links that mixes all the items of "animals" custom post type in all languages, like this:

"Adler, águila, cat, eagle, Haifisch, gato, Katze, león, lion, Löwe, shark, tiburón". 
 There is this Polylang plugin function that returns the current language: pll_current_language('slug') but, given my minimal knowledge of PHP, I cannot still figure it out how to use it to add a conditional to the code above that displays the right sequence of next/previous links for each language:

DE: "Adler, Haifisch, Katze, Löwe". EN: "cat, eagle, lion, shark".
 ES: "águila, gato, león, tiburón".

Any help would be much much much appreciated. Thanks!

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.