Modificare le colonne visualizzate nel backend wordpress

Partiamo dall’ipotesi di avere un post-type “crociera” con custom fileds “data_crociera” (che rappresenta la data di partenza della crociera), “compagnia_crociera” e “tipologia_crociera”; la Compagnia di crociera a sua volta è un post-type; la Tipologia di crociera invece è una taxonomy.
Vogliamo che nel backend di worpress cliccando sul post type “Crociera” vengano mostrati anzichè i campi default “Titolo del post”, “data di pubblicazione”, “autore” i campi “Titolo della crociera” (post title), “compagnia_crociera” (custom-field/post-type), “tipologia_crociera” (custom-field/taxonomy), “compagnia_crociera”  (custom-field/taxonomy).

Nell’esempio nei “vari manage_edit-crociera_columns” crociera è proprio il nome del post-type.

[php]
add_filter( ‘manage_edit-crociera_columns’, ‘my_edit_crociera_columns’ ) ;

function my_edit_crociera_columns( $columns ) {

$columns = array(
‘cb’ => ‘<input type="checkbox" />’,
‘title’ => __( ‘Titolo della crociera’ ),
‘data_crociera’ => __( ‘Data di partenza’ ),
‘compagnia_crociera’ => __( ‘Compagnia’ ),
‘tipologia_crociera’ => __( ‘Tipologia’ )
);

return $columns;
}
add_action( ‘manage_crociera_posts_custom_column’, ‘my_manage_crociera_columns’, 10, 2 );

function my_manage_crociera_columns( $column, $post_id ) {
global $post;

switch( $column ) {

case ‘data_crociera’ :
/* Get the post meta. */
$data_crociera = get_post_meta( $post_id, ‘data_crociera’, true );

/* If no data_crociera is found, output a default message. */
if ( empty( $data_crociera ) )
echo __( ‘Unknown’ );

/* If there is a data_crociera, append ‘minutes’ to the text string. */
else
printf(displaydata($data_crociera) );
break;

case ‘compagnia_crociera’ :
$array_compagnia = get_field(‘compagnia_crociera’); //nell’array c’è TUTTO il post
$compagnia = $array_compagnia->post_title;
$idcompagnia = $array_compagnia->ID;
$logocompagnia = get_field(‘logo_compagnia’, $idcompagnia);
printf($compagnia);
break;

case ‘tipologia_crociera’ :
//$tipologie = strip_tags( get_the_term_list($post->ID, ‘tipologia’), ‘ ‘ ); //taxonomy
//printf($tipologie);
/* Get the genres for the post. */
$terms = get_the_terms( $post_id, ‘tipologia’ );

/* If terms were found. */
if ( !empty( $terms ) ) {
$out = array();
/* Loop through each term, linking to the ‘edit posts’ page for the specific term. */
foreach ( $terms as $term ) {
$out[] = sprintf( ‘<a href="%s">%s</a>’,
esc_url( add_query_arg( array( ‘post_type’ => $post->post_type, ‘tipologia’ => $term->slug ), ‘edit.php’ ) ),
esc_html( sanitize_term_field( ‘name’, $term->name, $term->term_id, ‘tipologia’, ‘display’ ) )
);
}
/* Join the terms, separating them with a comma. */
echo join( ‘, ‘, $out );
}
/* If no terms were found, output a default message. */
else {
_e( ‘No Tipologia’ );
}
break;

/* Just break out of the switch statement for everything else. */
default :
break;
}
}

add_filter( ‘manage_edit-crociera_sortable_columns’, ‘my_crociera_sortable_columns’ );

function my_crociera_sortable_columns( $columns ) {

$columns['data_crociera'] = ‘data_crociera’;

return $columns;
}

/* Only run our customization on the ‘edit.php’ page in the admin. */
add_action( ‘load-edit.php’, ‘my_edit_crociera_load’ );

function my_edit_crociera_load() {
add_filter( ‘request’, ‘my_sort_crociera’ );
}

/* Sorts the crociera. */
function my_sort_crociera( $vars ) {

/* Check if we’re viewing the ‘crociera’ post type. */
if ( isset( $vars['post_type'] ) && ‘crociera’ == $vars['post_type'] ) {

/* Check if ‘orderby’ is set to ‘data_crociera’. */
if ( isset( $vars['orderby'] ) && ‘data_crociera’ == $vars['orderby'] ) {

/* Merge the query vars with our custom variables. */
$vars = array_merge(
$vars,
array(
‘meta_key’ => ‘data_crociera’,
‘orderby’ => ‘meta_value_num’
)
);
}
}

return $vars;
}
[/php]

Fonte

I commenti sono chiusi.