最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - "add_post_type_support" with Custom Post Type & ACF

programmeradmin1浏览0评论

I have a Custom Post Type named conferences (same slug, supports custom-fields), which is using the Advanced Custom Fields plugin.

One of those custom fields is "conference_date", which I want to show in the wp-admin/edit.php?post_type=conferences path as a column just like "title" or "date" is being shown by default. Is there any way to do it using add_post_type_support() or similar?

I already tried with this, which does not work:

add_action('init', 'show_conference_date', 100);

function show_conference_date() {
    add_post_type_support('conferences', 'custom-fields');
}

EDIT:

Solved, just add (and edit) this to your functions.php file:

// Add new table headings for "conferences" Custom Post Type
function set_columns_head_for_conferences($defaults) {
    $defaults['conference_date'] = 'When';
    $defaults['taxonomy-locations'] = 'Where';
    $defaults['conference_live'] = 'Live now?';
    return $defaults;
}

add_filter('manage_conferences_posts_columns', 'set_columns_head_for_conferences');

And set the column content like this:

// Example from: /
// Set values to columns
function conferences_custom_column_values( $column, $post_id ) {
    switch ( $column ) {
        // in this example, "conferences" has custom fields called 'conference_date' and 'conference_live'
        case 'conference_date'   :
            the_field('conference_date', $post_ID);
        break;

        case 'conference_live' :
            if (get_field('conference_live', $post_ID)) { //This is a "boolean" in my ACF 
                $conference_live_human_readable = "Yes";
            } else {
                $conference_live_human_readable = "No";
            }

            echo $conference_live_human_readable;
        break;

    }
}

add_action( 'manage_conferences_posts_custom_column' , 'conferences_custom_column_values', 10, 2 );

If you want to change the new column order, use this:

function column_order($columns) {
  $n_columns = array();
  $move = 'conference_date'; // what to move
  $before = 'conference_live'; // move before this
  foreach($columns as $key => $value) {
    if ($key==$before){
      $n_columns[$move] = $move;
    }
      $n_columns[$key] = $value;
  }
  return $n_columns;
}

add_filter('manage_conferences_posts_columns', 'column_order', 100);

You can debug all the $before options using return print_r($columns) instead of return $n_columns.

Default options for $before are:

  • 'cb' : checkbox for selecting post items for bulk actions
  • 'title' : displays the post title as well as post action links (edit, quick edit, trash, view) based on user permissions
  • 'author' : displays the username of the post author as a link to filter post by author
  • 'categories' : displays the post categories as links to filter post by category
  • 'tags' : displays the post tags as links to filter post by tags
  • 'comments' : displays a comment icon with the number of comments as a permalink to manage the comments for that post
  • 'date' : displays the date and status of the post

The last piece of code to order the new column is from: /

Thanks to Alexander Holsgrove for its response.

I have a Custom Post Type named conferences (same slug, supports custom-fields), which is using the Advanced Custom Fields plugin.

One of those custom fields is "conference_date", which I want to show in the wp-admin/edit.php?post_type=conferences path as a column just like "title" or "date" is being shown by default. Is there any way to do it using add_post_type_support() or similar?

I already tried with this, which does not work:

add_action('init', 'show_conference_date', 100);

function show_conference_date() {
    add_post_type_support('conferences', 'custom-fields');
}

EDIT:

Solved, just add (and edit) this to your functions.php file:

// Add new table headings for "conferences" Custom Post Type
function set_columns_head_for_conferences($defaults) {
    $defaults['conference_date'] = 'When';
    $defaults['taxonomy-locations'] = 'Where';
    $defaults['conference_live'] = 'Live now?';
    return $defaults;
}

add_filter('manage_conferences_posts_columns', 'set_columns_head_for_conferences');

And set the column content like this:

// Example from: https://developer.wordpress/reference/hooks/manage_post-post_type_posts_custom_column/
// Set values to columns
function conferences_custom_column_values( $column, $post_id ) {
    switch ( $column ) {
        // in this example, "conferences" has custom fields called 'conference_date' and 'conference_live'
        case 'conference_date'   :
            the_field('conference_date', $post_ID);
        break;

        case 'conference_live' :
            if (get_field('conference_live', $post_ID)) { //This is a "boolean" in my ACF 
                $conference_live_human_readable = "Yes";
            } else {
                $conference_live_human_readable = "No";
            }

            echo $conference_live_human_readable;
        break;

    }
}

add_action( 'manage_conferences_posts_custom_column' , 'conferences_custom_column_values', 10, 2 );

If you want to change the new column order, use this:

function column_order($columns) {
  $n_columns = array();
  $move = 'conference_date'; // what to move
  $before = 'conference_live'; // move before this
  foreach($columns as $key => $value) {
    if ($key==$before){
      $n_columns[$move] = $move;
    }
      $n_columns[$key] = $value;
  }
  return $n_columns;
}

add_filter('manage_conferences_posts_columns', 'column_order', 100);

You can debug all the $before options using return print_r($columns) instead of return $n_columns.

Default options for $before are:

  • 'cb' : checkbox for selecting post items for bulk actions
  • 'title' : displays the post title as well as post action links (edit, quick edit, trash, view) based on user permissions
  • 'author' : displays the username of the post author as a link to filter post by author
  • 'categories' : displays the post categories as links to filter post by category
  • 'tags' : displays the post tags as links to filter post by tags
  • 'comments' : displays a comment icon with the number of comments as a permalink to manage the comments for that post
  • 'date' : displays the date and status of the post

The last piece of code to order the new column is from: https://www.isitwp/change-wordpress-admin-post-columns-order/

Thanks to Alexander Holsgrove for its response.

Share Improve this question edited Apr 25, 2019 at 21:52 Jimmy Adaro asked Apr 25, 2019 at 19:26 Jimmy AdaroJimmy Adaro 1137 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You need to use filters to hook into manage_posts_columns and manage_posts_custom_column which will let you add the table heading and dates. The filters are slightly special as you need to include the name of your custom post type

manage_{$post_type}_posts_columns manage_{$post_type}_posts_custom_column

where {$post_type} in your case is conferences

// Add the table heading
function conf_columns_head($defaults) {
    $defaults['conference_date'] = 'Date';
    return $defaults;
}

// Add the conference date
function conf_columns_content($column_name, $post_ID) {
    if ($column_name == 'featured_image') {
        echo get_field('conference_date', $post_ID)
    }
}

add_filter('manage_conferences_posts_columns', 'conf_columns_head');
add_action('manage_conferences_posts_custom_column', 'conf_columns_content', 10, 2);

Untested code, but that should get you started.

发布评论

评论列表(0)

  1. 暂无评论