Last active
December 5, 2023 20:04
-
-
Save rveitch/245488ee1226fb26a52d to your computer and use it in GitHub Desktop.
Get next and prev posts in Wordpress by alphabetical order.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?PHP | |
/* | |
* Sort Next/Previous Post Link Buttons Alphabetically | |
*/ | |
function filter_next_post_sort($sort) { | |
if (get_post_type($post) == 'portfolio_page') { | |
$sort = "ORDER BY p.post_title ASC LIMIT 1"; | |
} | |
else{ | |
$sort = "ORDER BY p.post_date ASC LIMIT 1"; | |
} | |
return $sort; | |
} | |
function filter_next_post_where($where) { | |
global $post, $wpdb; | |
if (get_post_type($post) == 'portfolio_page') { | |
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) == 'portfolio_page') { | |
$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) == 'portfolio_page') { | |
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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, using this code and it works great. Where can I add $excluded_terms so that I can have the prev/next skip posts in certain categories? Thanks in advance for you help.