3

I found a snippet, which order all posts alphabetically. I wonder if is there a way to make it working just for one post-type? I have some custom post type in my website, and i want one of them to be ordered alphabetically, but the rest by date.

I changed filter_next_post_sort and fiter_previous_post_sort like this:

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

I hope it's good like that. But i need to change filter_next_post_where and filter_previous_post_where as well, but i don't find the right way.

Now they look like this:

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

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

Any suggestion, what to change? Thanks!

EDITED: I tried this, but it's not working:

function filter_next_post_where($where) {
    global $post, $wpdb;
    if (get_post_type($post) == 'szinesz') {
        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_title);
    }
}

Oh, and i use WP 4.1.1.

2

Find the solution: The hole snippet looks like this:

<?php
function filter_next_post_sort($sort) {
    global $post;
    if (get_post_type($post) == 'MyCustomPostType') {
        $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) == 'MyCustomPostType') {
        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) == 'MyCustomPostType') {
        $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) == 'MyCustomPostType') {
        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');
  • 1
    This solution is broken. You need to delete the references to $post->post_type and you need to add the globals to every four of your functions. I wanted to use your example and php output some errors, so I fixed them. If someone wants a revised code, I can add it, but I think it would just be better to edit this solution, as it is already marked as sufficient. – Alexander Trust Nov 10 at 23:47

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.