简体   繁体   中英

Add ACF admin column to custom post type

I'm trying to add a new admin column to my custom post type using the content of a custom field (ACF). The field I want to add is a 'post object' field, but it just displays the post title instead of the linked post from the ACF. I added a screenshot.

我现在拥有的

This is what I have so far:

function add_new_realisaties_column($columns) {
    $columns['realisatie_line'] = 'Line';
    return $columns;
}
add_filter('manage_realisaties_posts_columns', 'add_new_realisaties_column');

function add_new_realisaties_admin_column_show_value( $column, $post_id ) {
if ($column == 'realisatie_line') {
    $post_object = get_field('realisatie_line');

    if( $post_object ):
        // override $post
        $post = $post_object;
        setup_postdata( $post ); 

        $evdate = the_title();

        wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; 

    echo $evdate;
    }
}
add_filter('manage_realisaties_posts_custom_column', 'add_new_realisaties_admin_column_show_value', 10, 2);

/* Make the column sortable */
function set_custom_realisaties_sortable_columns( $columns ) {
    $columns['realisatie_line'] = 'realisatie_line';
    return $columns;
}
add_filter( 'manage_edit-realisaties_sortable_columns', 'set_custom_realisaties_sortable_columns' );

function realisaties_custom_orderby( $query ) {
    if ( ! is_admin() )
        return;
        $orderby = $query->get('orderby');

        if ( 'realisatie_line' == $orderby ) {
            $query->set( 'meta_key', 'realisatie_line' );
            $query->set( 'orderby', 'meta_value' );
        }
    }
add_action( 'pre_get_posts', 'realisaties_custom_orderby' );

Try changing your add_new_realisaties_admin_column_show_value function to the code below. If the ACF field name is realisatie_line you are going to need to also pass a $post_id to get the specific meta data back for each particular post.

function add_new_realisaties_admin_column_show_value( $column, $post_id ) {

    //Try using a switch/case for the column name
    switch ( $column ) {
       case 'realisatie_line': //Name of new column from add_new_realisaties_column function
          echo get_the_title( get_post_meta( $post_id, 'realisatie_line', true ) ); //Getting the ACF post meta using the $post_id, passing it through the get_the_title function to get title of attached post
          break;

       default:
          break;

    }
}

There's a couple things I notice. One is that you shouldn't actually need to use the setup_postdata() function, because the ACF Post Object field uses get_post() which returns a full object already. You'd only do this if you're intended to override the global $post object, say on a single-{post_type}.php template, for instance.

Another thing is that it's generally more common to use a switch statement instead of an if/else for post columns. A bit pedantic, but something to note.

Lastly, the_title() will echo the title by default, so assigning it, and then echoing it later, can cause issues (namely leaving variables littered around the document). Consider using get_the_title() if you plan on assigning it to a variable. Also, I won't go into excruciating detail, but just using setup_postdata may not be enough to get the post helper functions to pull the data from where you want.

Now, with all that said, you should be able to just echo the post_title field of the $post_object fromget_field() , since it returns a full formed WP_Post object . I put this on a test site of mine and it worked just as intended:

add_filter('manage_realisaties_posts_custom_column', 'add_new_realisaties_admin_column_show_value', 10, 2);
function add_new_realisaties_admin_column_show_value( $column, $post_id ){
    switch( $column ){
        case 'realisatie_line':
            if( $post_object = get_field('realisatie_line') ){
                echo $post_object->post_title;
            }
            break;
    }
}

And here's what it looks like in the admin, note I just grabbed a random post for the Post Object relational field:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM