-
-
Save KoljaL/c27ff76ca978066995de8c2eeb4d9bf2 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