I have three custom post types (brewery-profile, supplier-profile, brewery-trail) and I would like them to be ordered alphabetically so that the post navigation shows the previous/next post in alphabetical order. However, I would still like my regular blog posts to be ordered by date.
Based on this thread Iâve been able to get it working for a single custom post type using the following code:
function filter_next_post_sort($sort) {
global $post;
if (get_post_type($post) == âbrewery-profileâ) {
$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) == âbrewery-profileâ) {
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);
}
}
function filter_previous_post_sort($sort) {
global $post;
if (get_post_type($post) == âbrewery-profileâ) {
$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) == âbrewery-profileâ) {
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);
}
}
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â);
How can I add the other two custom post types without affecting the default Post type?