web-dev-qa-db-fra.com

poste suivant et précédent lien ordonné par ordre alphabétique

J'ai trouvé un extrait , qui ordonne tous les articles par ordre alphabétique. Je me demande s'il y a un moyen de le faire fonctionner uniquement pour un post-type? J'ai un type de message personnalisé sur mon site Web, et je veux que l'un d'eux soit classé par ordre alphabétique, mais le reste par date.

J'ai changé filter_next_post_sort et fiter_previous_post_sort comme ceci:

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;
}

J'espère que c'est bien comme ça. Mais je dois aussi changer filter_next_post_where et filter_previous_post_where, mais je ne trouve pas le bon chemin.

Maintenant, ils ressemblent à ceci:

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);
}

Toute suggestion, quoi changer? Merci!

EDITED: J'ai essayé cela, mais ça ne marche pas:

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, et j'utilise WP 4.1.1.

3
Koli

Trouvez la solution: l'extrait de trou ressemble à ceci:

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;
}
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, $post->post_type );
    }
}

function filter_previous_post_sort($sort) {
    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, $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');
1
Koli