Come ordinare i post di un tipo di post personalizzato per data DESC nel pannello di amministrazione?

17 gen 2013, 11:00:33
Visualizzazioni: 32.2K
Voti: 9

Ho creato un nuovo tipo di post chiamato "Video".

Quando creo un post per questo tipo di contenuto, i post vengono ordinati per title ASC.

È possibile ordinare i post per data in ordine decrescente (DESC)?

register_post_type('Videos', array(
    'labels' => array(
        'name' => _x('Videos', 'nome generale del tipo di post'),
        'singular_name' => _x('Video', 'nome singolare del tipo di post'),
        'add_new' => _x('Aggiungi', 'Video'),
        'add_new_item' => __('Aggiungi un video'),
        'edit_item' => __('Modifica video'),
        'new_item' => __('Nuovo video'),
        'view_item' => __('Visualizza link del video'),
        //'search_items' => __(' Video'),
        'menu_name' => 'Video'
    ),
    'public' => true,
    'show_ui' => true,
    'capability_type' => 'post',
    'hierarchical' => true,
    'rewrite' => array('slug' => 'video'),
    'query_var' => true,
    'supports' => array(
        'title',
        'editor' => false,
        'excerpt' => false,
        'trackbacks' => false,
        'custom-fields',
        'comments' => false,
        'revisions' => false,
        'thumbnail' => false,
        'author' => false,
        'page-attributes' => false,
    ),
    'taxonomies' => array('post_tag')
   )
 );
2
Commenti

Per favore aggiungi il codice dove mostri i post, non aggiungi il parametro order_by quando registri il post_type ma solo quando esegui la query per i post

Pontus Abrahamsson Pontus Abrahamsson
17 gen 2013 16:25:57

Oh scusa, vorrei aggiungere l'order_by nell'amministrazione di Wordpress

Steffi Steffi
17 gen 2013 18:07:57
Tutte le risposte alla domanda 4
3
19

Va bene, puoi semplicemente agganciarti al filtro pre_get_posts e verificare is_admin. Inserisci questo nel tuo tema o plugin:

function wpse_81939_post_types_admin_order( $wp_query ) {
  if (is_admin()) {

    // Ottieni il post type dalla query
    $post_type = $wp_query->query['post_type'];

    if ( $post_type == 'Videos') {

      $wp_query->set('orderby', 'date');

      $wp_query->set('order', 'DESC');
    }
  }
}
add_filter('pre_get_posts', 'wpse_81939_post_types_admin_order');

Consiglierei anche di cambiare il post_type "Videos" in minuscolo come "video".

17 gen 2013 19:04:14
Commenti

Ok, grazie Pontus! Proverò questa soluzione. Ma perché non è impostato di default? Perché per i "Posts" è già impostato con "orderby DESC"

Steffi Steffi
17 gen 2013 23:40:26

Ha funzionato per te?

Pontus Abrahamsson Pontus Abrahamsson
18 gen 2013 10:15:42

non è di default perché è impostato su "Name" in ordine alfabetico di default (o credo sullo slug)

Nabeel Khan Nabeel Khan
4 mag 2016 01:51:22
0
10

L'esempio sopra disabilita la funzionalità di ordinamento cliccando sulle colonne.

Ordinabile per più tipi di post personalizzati:

function wpse_819391_post_types_admin_order( $wp_query ) {
  if ( is_admin() && !isset( $_GET['orderby'] ) ) {     
    // Ottiene il tipo di post dalla query
    $post_type = $wp_query->query['post_type'];
    if ( in_array( $post_type, array('videos','news','text') ) ) {
      $wp_query->set('orderby', 'date');
      $wp_query->set('order', 'DESC');
    }
  }
}
add_filter('pre_get_posts', 'wpse_819391_post_types_admin_order');
10 dic 2013 01:40:23
0

Ho utilizzato un approccio leggermente diverso:

    add_action('pre_get_posts', 'filter_posts_list'); 

    function filter_posts_list($query)  {
        //$pagenow contiene il nome della pagina corrente visualizzata
         global $pagenow, $typenow;  
        if(current_user_can('edit_posts') && ('edit.php' == $pagenow))  { 
            //metodo set() globale $query per l'impostazione
            $query->set('orderby', 'date');
            $query->set('order', 'desc');
        }
    }
28 nov 2019 13:39:20
0

Cambia l'argomento hierarchical a false o elimina quella riga per affidarti al valore predefinito di false.

'hierarchical' => false,

https://developer.wordpress.org/reference/functions/register_post_type/#parameters

15 giu 2024 18:38:56