How to add custom fields to the custom post listing columns in the WordPress admin post listings

Sdílet
Vložit
  • čas přidán 7. 09. 2024

Komentáře • 4

  • @BruceYoung
    @BruceYoung  Před rokem +5

    add_filter( 'manage_posts_columns' , 'by_manage_posts_columns' );
    add_filter( 'manage_post_posts_custom_column', 'by_manage_post_posts_custom_column', 10, 2 );
    add_filter( 'manage_directory_posts_custom_column', 'by_manage_post_posts_custom_column', 10, 2 );
    /*
    function by_manage_posts_columns( $post_columns ) {
    $post_columns['img'] = 'Featured Image';
    return $post_columns;
    }
    */
    function by_manage_posts_columns( $post_columns) {
    $post_type = get_post_type();
    if ( $post_type == 'post' ) {
    $post_columns = array_slice( $post_columns, 0, 1 , true ) + array ( 'img' => 'Featured Image' ) + array_slice( $post_columns, 1, count( $post_columns ), true);
    return $post_columns;
    }
    if( $post_type == 'directory' ) {
    $post_columns = array_slice( $post_columns, 0, 1 , true ) + array ( 'img' => 'Featured Image' ) + array_slice( $post_columns, 1, 1, true) + array ( 'contcustom' => 'Custom Value' ) + array ( 'contcity' => 'City' ) + array ( 'contnumber' => 'Contact Number' ) + array_slice( $post_columns, 2, 1, true) ;
    // $post_columns['contnumber'] = 'Contact Number';
    return $post_columns;

    }

    return $post_columns;
    }
    function by_manage_post_posts_custom_column( $column_name, $post_id ) {
    if( $column_name == 'img' ) {
    echo get_the_post_thumbnail( $post_id, 'thumbnail' );
    }
    if( $column_name == 'contcity' ) {
    echo get_field( 'contact_city' ) ;
    }
    if( $column_name == 'contnumber' ) {
    echo get_field( 'contact_number' ) ;
    }
    if( $column_name == 'contcustom' ) {
    echo get_field( 'contact_custom_field' ) ;
    }


    return $column_name;
    }

  • @numevalnumeval7733
    @numevalnumeval7733 Před rokem

    Thank you very much !

  • @seanh1595
    @seanh1595 Před rokem

    Excellent stuff, loved the last couple of ones, makes a huge difference being able to see some of the fields in admin rather than having to manually load up a post. Any idea on how this can be achieved with relationships? I have multiple post types tied together using relationships in Metabox and this would provide an excellent way of quickly looking at them.

    • @seanh1595
      @seanh1595 Před rokem

      Being able to filter it as well, or filter any of the custom fields is a great option. How can the manage_posts_columns be turned into an active filter like you can with title or date?