WordPress admin post list modifizieren: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Markierung: Manuelle Zurücksetzung |
|||
| (3 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt) | |||
| Zeile 12: | Zeile 12: | ||
# Custom Column(s) anzeigen | # Custom Column(s) anzeigen | ||
=== Beispielcode um Custom Column(s) zu registrieren === | |||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
| Zeile 38: | Zeile 38: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Beispielcode um Custom Column(s) anzuzeigen === | |||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
| Zeile 48: | Zeile 48: | ||
switch ( $column ) { | switch ( $column ) { | ||
// | // Display a thumbnail photo | ||
case 'photo' : | case 'photo' : | ||
echo get_the_post_thumbnail( $post_id, 'thumbnail' ); | echo get_the_post_thumbnail( $post_id, 'thumbnail' ); | ||
break; | break; | ||
// | // Display a list of the custom taxonomy terms assigned to the post | ||
case 'custom_taxonomy' : | case 'custom_taxonomy' : | ||
$terms = get_the_term_list( $post_id , 'my_custom_taxonomy' , '' , ', ' , '' ); | $terms = get_the_term_list( $post_id , 'my_custom_taxonomy' , '' , ', ' , '' ); | ||
| Zeile 59: | Zeile 59: | ||
break; | break; | ||
// | // Display the value of an ACF (Advanced Custom Fields) field | ||
case 'acf_field' : | case 'acf_field' : | ||
echo get_field( 'my_acf_field', $post_id ); | echo get_field( 'my_acf_field', $post_id ); | ||
break; | break; | ||
// | // Display the value of an ACF (Advanced Custom Fields) array field | ||
case 'acf_field' : | case 'acf_field' : | ||
$var = get_field( 'my_acf_field', $post_id ); | $var = get_field( 'my_acf_field', $post_id ); | ||
echo implode(", ", $var); | echo implode(", ", $var); | ||
break; | |||
// Display the value of an related ACF (Advanced Custom Fields) field of an related Post Object | |||
case 'location-title' : | |||
echo get_the_title(get_field( 'contact-location', $post_id)); | |||
break; | |||
// Display the value of an related ACF (Advanced Custom Fields) field of an ACF field | |||
case 'location-country' : | |||
echo get_field('location-country',get_field( 'contact-location', $post_id)); | |||
break; | break; | ||
} | } | ||
| Zeile 73: | Zeile 83: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
siehe auch: https://developer.wordpress.org/reference/hooks/manage_posts_custom_column/ | |||
== Spalte der Listenansicht des Custom Post Type sortierbar machen == | == Spalte der Listenansicht des Custom Post Type sortierbar machen == | ||
| Zeile 92: | Zeile 104: | ||
https://www.ractoon.com/wordpress-sortable-admin-columns-for-custom-posts/ | https://www.ractoon.com/wordpress-sortable-admin-columns-for-custom-posts/ | ||
== Spalte der Listenansicht des Custom Post Type alphabetisch sortieren == | |||
<syntaxhighlight lang="php"> | |||
// Order custom columns in WordPress Backend CPT Listing alphabetically | |||
add_filter( 'manage_edit-reference_sortable_columns', 'reference_sortable_columns' ); | |||
function reference_sortable_columns( $columns ) { | |||
$columns['reference_customer_firma'] = 'reference_customer_firma'; | |||
return $columns; | |||
} | |||
add_action( 'pre_get_posts', 'reference_firma_orderby' ); | |||
function reference_firma_orderby( $query ) { | |||
if( ! is_admin() ) | |||
return; | |||
$orderby = $query->get( 'orderby'); | |||
if( 'reference_customer_firma' == $orderby ) { | |||
$query->set('meta_key','reference_customer_firma'); | |||
$query->set('orderby','meta_value'); | |||
// "meta_value_num" is used for numeric sorting | |||
// "meta_value" is used for Alphabetically sort. | |||
} | |||
} | |||
</syntaxhighlight> | |||
https://gist.github.com/vishalkakadiya/8c997a5e8789147e012b12cc4af90389 | |||
== Spalte der Listenansicht des Custom Post Type filterbar machen == | == Spalte der Listenansicht des Custom Post Type filterbar machen == | ||
Aktuelle Version vom 2. Mai 2022, 17:17 Uhr
Allgemein[Bearbeiten]
Spalte der Listenansicht des Custom Post Type hinzufügen[Bearbeiten]
https://www.ractoon.com/wordpress-sortable-admin-columns-for-custom-posts/
Um die Custom Fields eines Custom Post Type anzuzeigen sind zwei Schritte notwendig:
- Custom Column(s) registrieren
- Custom Column(s) anzeigen
Beispielcode um Custom Column(s) zu registrieren[Bearbeiten]
// Register custom columns to WordPress Backend CPT Listing
add_filter( 'manage_mycpt_posts_columns', 'set_custom_edit_mycpt_columns' );
function set_custom_edit_mycpt_columns( $columns ) {
// unset to order it last
unset($columns['date']);
// register custom columns
$columns['photo'] = __( 'Photo', 'my-text-domain' );
$columns['custom_taxonomy'] = __( 'Custom Taxonomy', 'my-text-domain' );
$columns['acf_field'] = __( 'ACF Field', 'my-text-domain' );
// end register custom columns
// ready to order it last
$columns['date'] = $date;
return $columns;
}
Beispielcode um Custom Column(s) anzuzeigen[Bearbeiten]
// Display custom columns in WordPress Backend CPT Listing
add_action( 'manage_mycpt_posts_custom_column' , 'custom_mycpt_column', 10, 2 );
function custom_mycpt_column( $column, $post_id ) {
switch ( $column ) {
// Display a thumbnail photo
case 'photo' :
echo get_the_post_thumbnail( $post_id, 'thumbnail' );
break;
// Display a list of the custom taxonomy terms assigned to the post
case 'custom_taxonomy' :
$terms = get_the_term_list( $post_id , 'my_custom_taxonomy' , '' , ', ' , '' );
echo is_string( $terms ) ? $terms : '—';
break;
// Display the value of an ACF (Advanced Custom Fields) field
case 'acf_field' :
echo get_field( 'my_acf_field', $post_id );
break;
// Display the value of an ACF (Advanced Custom Fields) array field
case 'acf_field' :
$var = get_field( 'my_acf_field', $post_id );
echo implode(", ", $var);
break;
// Display the value of an related ACF (Advanced Custom Fields) field of an related Post Object
case 'location-title' :
echo get_the_title(get_field( 'contact-location', $post_id));
break;
// Display the value of an related ACF (Advanced Custom Fields) field of an ACF field
case 'location-country' :
echo get_field('location-country',get_field( 'contact-location', $post_id));
break;
}
}
siehe auch: https://developer.wordpress.org/reference/hooks/manage_posts_custom_column/
Spalte der Listenansicht des Custom Post Type sortierbar machen[Bearbeiten]
Beispielcode um Custom Column(s) sortierbar zu machen
add_filter( 'manage_edit-mycpt_sortable_columns', 'set_custom_mycpt_sortable_columns' );
function set_custom_mycpt_sortable_columns( $columns ) {
$columns['custom_taxonomy'] = 'custom_taxonomy';
$columns['acf_field'] = 'acf_field';
return $columns;
}
https://www.ractoon.com/wordpress-sortable-admin-columns-for-custom-posts/
Spalte der Listenansicht des Custom Post Type alphabetisch sortieren[Bearbeiten]
// Order custom columns in WordPress Backend CPT Listing alphabetically
add_filter( 'manage_edit-reference_sortable_columns', 'reference_sortable_columns' );
function reference_sortable_columns( $columns ) {
$columns['reference_customer_firma'] = 'reference_customer_firma';
return $columns;
}
add_action( 'pre_get_posts', 'reference_firma_orderby' );
function reference_firma_orderby( $query ) {
if( ! is_admin() )
return;
$orderby = $query->get( 'orderby');
if( 'reference_customer_firma' == $orderby ) {
$query->set('meta_key','reference_customer_firma');
$query->set('orderby','meta_value');
// "meta_value_num" is used for numeric sorting
// "meta_value" is used for Alphabetically sort.
}
}
https://gist.github.com/vishalkakadiya/8c997a5e8789147e012b12cc4af90389
Spalte der Listenansicht des Custom Post Type filterbar machen[Bearbeiten]
- https://wordpress.stackexchange.com/questions/45436/add-filter-menu-to-admin-list-of-posts-of-custom-type-to-filter-posts-by-custo
- getestet und funktioniert mit definierten Werten (Label-Value-Paar)
- Übergabe aller ACF-Feldwerte eines Feldes (z.B.: 'kontakt-typ') als dynamisches Label-Value-Paar als Array bisher nicht geklappt
- https://wordpress.stackexchange.com/questions/212519/filter-by-custom-field-in-custom-post-type-on-admin-page
- getestet und Filter wird angezeigt, aber er filtert nicht bei Auswahl
- hier werden die Werte zum filtern dynamisch/automatisch in das Dropdown geparsed