1

I've got a True/False value added to posts (Exclusive versus Curated) that was added via Advanced Custom Fields. How do I get that value to show up as a column on the Post list (/wp-admin/edit.php) to allow editors to quickly sort via that field?

I found this example (http://code.tutsplus.com/articles/add-a-custom-column-in-posts-and-custom-post-types-admin-screen--wp-24934) for adding the post thumbnail, but do not know how to adjust to pull and ACF field in instead.

Field name is company_exclusive and type is True/False.

Any pointers would be appreciated.

Edit: I found this solution (http://olliebarker.co.uk/articles/2014/06/displaying-custom-fields-wordpress-admin-post-lists/), and adapted its code to my needs, but after creating this, I'm not seeing the fields in on wp-admin/edit.php screen (either in Screen options or visible). I'm adding it to my theme's functions.php. That is the right place, correct?

//Adds ACF fields to Post List
add_filter('posts_columns', 'custom_posts_table_head');
function custom_posts_table_head( $columns ) {

    $columns['author_name']  = 'Author Name';
    $columns['company_exclusive']  = 'Company Exclusive?';
    $columns['region']  = 'Region';
    $columns['article_excerpt_title']  = 'Article Excerpt Title';
    return $columns;

}
add_action( 'posts_columns', 'custom_posts_table_content', 10, 2);

function bs_projects_table_content( $column_name, $post_id ) {

    if( $column_name == 'author_name' ) {
        $author_name = get_post_meta( $post_id, 'author_name', true );
        echo $author_name;
    }
    if( $column_name == 'company_exclusive' ) {
        $company_exclusive = get_post_meta( $post_id, 'company_exclusive', true );
        if( $company_exclusive == '1' ) { echo 'Yes'; } else { echo 'No'; }
    }
    if( $column_name == 'region' ) {
        $region = get_post_meta( $post_id, 'region', true );
        echo $region;
    }
    if( $column_name == 'article_excerpt_title' ) {
        $article_excerpt_title = get_post_meta( $post_id, 'article_excerpt_title', true );
        echo $article_excerpt_title;
    }

}
4

You've got your action/filter names slightly off:

// For registering the column
add_filter( 'manage_posts_columns', 'custom_posts_table_head' );

// For rendering the column
add_action( 'manage_posts_custom_column', 'custom_posts_table_content', 10, 2 );
  • Simply changing those action/filter names lines, I get a slew of errors on the edit.php screen, starting with: Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'custom_posts_table_head' not found or invalid function name in /Applications/MAMP/htdocs-lenovo-health-wp/wp-includes/plugin.php on line 213 – Keefer Aug 13 '15 at 15:26
  • The error is pretty explanatory - are you sure you have the function in your code? – TheDeadMedic Aug 13 '15 at 15:38
  • New to this.. I just had the code in my original question in my theme's functions.php. Do I have to call the function somewhere within the wp-admin subdir as well? – Keefer Aug 14 '15 at 20:28
1

I used following code to specify featured products in admin panel product list.

add_filter('manage_product_posts_columns', 'hs_product_table_head');
function hs_product_table_head( $columns ) {
    $columns['product_featured']  = 'Featured';
    return $columns;

}
add_action( 'manage_product_posts_custom_column', 'hs_product_table_content', 10, 2 );

function hs_product_table_content( $column_name, $post_id ) {
    if( $column_name == 'product_featured' ) {
        $featured_product = get_post_meta( $post_id, 'featured_product', true );
        if($featured_product == 1) {
            echo "Yes";
        }
    }
}

To use this for other post types - change the following code. For example - post type is "portfolio"

add_filter('manage_portfolio_posts_columns', 'hs_portfolio_table_head');
add_action( 'manage_portfolio_posts_custom_column', 'hs_portfolio_table_content', 10, 2 );
0

For Advanced Custom Fields, put this code in functions.php:

add_filter( 'manage_faq_posts_columns', 'set_custom_edit_faq_columns' );    
add_action( 'manage_faq_posts_custom_column' , 'custom_faq_column', 10, 2 );

function set_custom_edit_faq_columns($columns) {    
    unset( $columns['author'] );
    $columns['is_useful'] = 'Is Useful';
    $columns['is_unless'] = 'Is Unless';
    return $columns;    
}

function custom_faq_column( $column, $post_id ) {   
    global $post;
    switch ( $column ) {
        case 'is_useful' :
            if(get_field( "is_useful", $post_id )) {
                echo get_field( "is_useful", $post_id );
            } else {
                echo 0;
            }
        break;

        case 'is_unless' :
            if(get_field( "is_unless", $post_id )) {
                echo get_field( "is_unless", $post_id );
            } else {
                echo 0;
            }
        break;    
    }   
}

function my_column_register_sortable( $columns ) {
     $columns['is_useful'] = 'is_useful';
    $columns['is_unless'] = 'is_unless';
    return $columns;
}

add_filter("manage_edit-faq_sortable_columns", "my_column_register_sortable" );
  • 1
    Welcome to WPSE. Please do not just dump code but explain it with code comments or a short paragraph about how it works. For you it is obvious, but someone else might not understand what it does just by looking at the code. – kraftner Mar 4 '16 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.