WordPress admin post list modifizieren: Unterschied zwischen den Versionen

Aus wiki.sehanka.de
Zur Navigation springen Zur Suche springen
Markierung: Zurückgesetzt
Markierung: Manuelle Zurücksetzung
Zeile 101: Zeile 101:
** getestet und Filter wird angezeigt, aber er filtert nicht bei Auswahl
** getestet und Filter wird angezeigt, aber er filtert nicht bei Auswahl
** hier werden die Werte zum filtern dynamisch/automatisch in das Dropdown geparsed
** hier werden die Werte zum filtern dynamisch/automatisch in das Dropdown geparsed
== Post title automatisch durch Werte der Custom fields ersetzen ==
* folgender Code kann direkt in die functions.php geschrieben werden
* besser und übersichtlicher ist, es in die cpt-BEZEICHNER.php des betroffenen Datenobjekts zu schreiben (im folgenden Beispiel wäre das die cpt-kontakte.php)
'''Beispielcode für Custom Post Type "kontakte"'''
<syntaxhighlight lang="php">
<?php
$cpt_contact_key = "kontakte";
add_filter( 'wp_insert_post_data' , function( $data , $postarr ) {
    global $cpt_contact_key;
    if (get_post_type() == $cpt_contact_key) {
        $data['post_title'] = $postarr['acf'][get_field('_vorname')].' '.$postarr['acf'][get_field('_nachname')];
    }
    return $data;
} , '99', 2 );
</syntaxhighlight>


= Quellen =
= Quellen =

Version vom 28. Januar 2022, 14:20 Uhr

Allgemein

Spalte der Listenansicht des Custom Post Type hinzufügen

https://www.ractoon.com/wordpress-sortable-admin-columns-for-custom-posts/

Um die Custom Fields eines Custom Post Type anzuzeigen sind zwei Schritte notwendig:

  1. Custom Column(s) registrieren
  2. Custom Column(s) anzeigen

Beispielcode um Custom Column(s) zu registrieren

// 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

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

Spalte der Listenansicht des Custom Post Type sortierbar machen

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 filterbar machen

Quellen