下一篇文章和上一篇文章链接按字母顺序排序

时间:2015-04-20 作者:Koli

我找到了一个snippet, 按字母顺序排列所有帖子。我想知道是否有办法让它只适用于一种职位类型?我在我的网站上有一些自定义的帖子类型,我希望其中一个按字母顺序排列,其余按日期排列。

我改变了filter_next_post_sortfiter_previous_post_sort 像这样:

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;
}
我希望这样好。但我需要改变filter_next_post_wherefilter_previous_post_where 当然,但我没有找到正确的方法。

现在它们看起来像这样:

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);
}
有什么建议,要改变什么?谢谢

编辑:我试过这个,但不起作用:

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);
    }
}
哦,我使用WP 4.1.1。

1 个回复
最合适的回答,由SO网友:Koli 整理而成

找到解决方案:孔片段如下所示:

<?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\');

结束

相关推荐

How to get order of posts?

如何获取完成查询的顺序?例如,我的woocommerce产品归档页面是根据order\\u by select框(如下所示)通过订购进行查询的。我怎样才能得到我的帖子的排序依据?例如,如果是默认排序,我想知道。如果是受欢迎程度,我想知道。如果是因为新鲜,我想知道。