{"id":14683,"date":"2022-07-21T13:14:10","date_gmt":"2022-07-21T11:14:10","guid":{"rendered":"https:\/\/hesmid.nl\/test\/?p=14683"},"modified":"2022-07-21T13:28:32","modified_gmt":"2022-07-21T11:28:32","slug":"adding-custom-columns-to-custom-post-types","status":"publish","type":"post","link":"https:\/\/hesmid.nl\/test\/adding-custom-columns-to-custom-post-types\/","title":{"rendered":"Adding custom columns to custom post types"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">The simple solution:<\/h2>\n\n\n\n<p><code>register_taxonomy()<\/code> now has support for that built in via the <code>show_admin_column<\/code> setting passed via arguments. <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/register_taxonomy\/\">developer.wordpress.org\/reference\/functions\/register_taxonomy<\/a>. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>'show_admin_column' =&gt; true,<\/code><\/pre>\n\n\n\n<p>If you aren&#8217;t registering the taxonomy yourself, you can still alter the args via the <code>register_taxonomy_args<\/code> filter <a href=\"https:\/\/developer.wordpress.org\/reference\/hooks\/register_taxonomy_args\/\">developer.wordpress.org\/reference\/hooks\/register_taxonomy_args<\/a><\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">If you want more control:<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.ractoon.com\/wordpress-sortable-admin-columns-for-custom-posts\/\">https:\/\/www.ractoon.com\/wordpress-sortable-admin-columns-for-custom-posts\/<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Modifying the Custom Columns for a Post Type<\/h4>\n\n\n\n<p>To modify the columns for a specific post type you can use the <a rel=\"noreferrer noopener\" href=\"https:\/\/codex.wordpress.org\/Plugin_API\/Filter_Reference\/manage_$post_type_posts_columns\" target=\"_blank\"><code>manage_${post_type}_posts_columns<\/code><\/a> filter. In your <code>functions.php<\/code> file add the following (replacing <code>mycpt<\/code> in the hook with your custom post type name used in the <a rel=\"noreferrer noopener\" href=\"https:\/\/codex.wordpress.org\/Function_Reference\/register_post_type\" target=\"_blank\"><code>register_post_type()<\/code><\/a> function):<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">add_filter( 'manage_mycpt_posts_columns', 'set_custom_edit_mycpt_columns' );\n\nfunction set_custom_edit_mycpt_columns( $columns ) {\n  $date = $columns['date'];\n  unset( $columns['date'] );\n\n  $columns['photo'] = __( 'Photo', 'my-text-domain' );\n  $columns['custom_taxonomy'] = __( 'Custom Taxonomy', 'my-text-domain' );\n  $columns['acf_field'] = __( 'ACF Field', 'my-text-domain' );\n\n  $columns['date'] = $date;\n\n  return $columns;\n}\n\n<\/pre>\n\n\n\n<p>The filter passes the current <code>$columns<\/code> available to the post type in an array. You can modify the array however you\u2019d like to add, remove, or modify what shows up in the table.<\/p>\n\n\n\n<p>The above code example does a couple things:<\/p>\n\n\n\n<ul><li>Modifies the default date column to move it to the end of the table<\/li><li>Adds a photo, custom taxonomy, and ACF field to the admin table display<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Adding Data to the New Columns<\/h4>\n\n\n\n<p>Now these columns will appear when viewing the admin listing for your custom post type, but there won\u2019t be any data in them. To assign values for each column you can use the <a href=\"https:\/\/www.ractoon.com\/wordpress-sortable-admin-columns-for-custom-posts\/manage_$%7Bpost_type%7D_posts_custom_column\"><code>manage_${post_type}_posts_custom_column<\/code><\/a> action. In your <code>functions.php<\/code> you would add:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">add_action( 'manage_mycpt_posts_custom_column' , 'custom_mycpt_column', 10, 2 );\n\nfunction custom_mycpt_column( $column, $post_id ) {\n  switch ( $column ) {\n\n    \/\/ display a thumbnail photo\n    case 'photo' :\n      echo get_the_post_thumbnail( $post_id, 'thumbnail' );\n      break;\n\n    \/\/ display a list of the custom taxonomy terms assigned to the post \n    case 'custom_taxonomy' :\n      $terms = get_the_term_list( $post_id , 'my_custom_taxonomy' , '' , ', ' , '' );\n      echo is_string( $terms ) ? $terms : '\u2014';\n      break;\n\n    \/\/ display the value of an ACF (Advanced Custom Fields) field\n    case 'acf_field' :\n      echo get_field( 'my_acf_field', $post_id );  \n      break;\n\n  }\n}<\/pre>\n\n\n\n<p>With the above code in place you should now have data displaying for each post. Nice!<\/p>\n\n\n\n<p>A handy feature of the WordPress admin tables is the ability to sort them by column data. You can do this with your custom columns as well with some additional code.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Making Your Custom Columns Sortable<\/h4>\n\n\n\n<p>To make your custom columns sortable you\u2019ll use the <code>manage_edit-${post_type}_sortable_columns<\/code> filter. In your <code>functions.php<\/code> file add the following:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">add_filter( 'manage_edit-mycpt_sortable_columns', 'set_custom_mycpt_sortable_columns' );\n\nfunction set_custom_mycpt_sortable_columns( $columns ) {\n  $columns['custom_taxonomy'] = 'custom_taxonomy';\n  $columns['acf_field'] = 'acf_field';\n\n  return $columns;\n}<\/pre>\n\n\n\n<p>All we\u2019re doing here is setting the custom columns that should be allowed to sort, in this case the custom taxonomy and our ACF field. Now the column headers for these columns will appear as links and be sortable just like default WordPress columns.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"sorting-metadata\"><a href=\"https:\/\/www.ractoon.com\/wordpress-sortable-admin-columns-for-custom-posts\/#sorting-metadata\"><\/a>Sorting Metadata<\/h4>\n\n\n\n<p>By default WordPress does a decent job of sorting the data, including taxonomies recently. However, for meta data (such as the ACF field we\u2019ve added) it needs some guidance. To get things working as expected you\u2019ll need to modify the query for the table data. For this we\u2019ll be using the <a href=\"https:\/\/codex.wordpress.org\/Plugin_API\/Action_Reference\/pre_get_posts\" target=\"_blank\" rel=\"noreferrer noopener\"><code>pre_get_posts<\/code><\/a> action. In your <code>functions.php<\/code> add:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">add_action( 'pre_get_posts', 'mycpt_custom_orderby' );\n\nfunction mycpt_custom_orderby( $query ) {\n  if ( ! is_admin() )\n    return;\n\n  $orderby = $query->get( 'orderby');\n\n  if ( 'acf_field' == $orderby ) {\n    $query->set( 'meta_key', 'acf_field' );\n    $query->set( 'orderby', 'meta_value_num' );\n  }\n}<\/pre>\n\n\n\n<p>Here we modify the query if it is sorting by the <code>acf_field<\/code> column. It modifies the query to use the meta key <code>acf_field<\/code> that the ACF field is using in the database, and is expecting a numeric value when we set <code>orderby<\/code> to <code>meta_value_num<\/code>. If your meta value is a string you\u2019ll want to set orderby to <code>meta_value<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The simple solution: register_taxonomy() now has support for that built in via the show_admin_column setting passed via arguments. developer.wordpress.org\/reference\/functions\/register_taxonomy. If you aren&#8217;t registering the taxonomy yourself, you can still alter the args via the register_taxonomy_args filter developer.wordpress.org\/reference\/hooks\/register_taxonomy_args<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[17],"tags":[35,768],"acf":[],"_links":{"self":[{"href":"https:\/\/hesmid.nl\/test\/wp-json\/wp\/v2\/posts\/14683"}],"collection":[{"href":"https:\/\/hesmid.nl\/test\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hesmid.nl\/test\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hesmid.nl\/test\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hesmid.nl\/test\/wp-json\/wp\/v2\/comments?post=14683"}],"version-history":[{"count":5,"href":"https:\/\/hesmid.nl\/test\/wp-json\/wp\/v2\/posts\/14683\/revisions"}],"predecessor-version":[{"id":14689,"href":"https:\/\/hesmid.nl\/test\/wp-json\/wp\/v2\/posts\/14683\/revisions\/14689"}],"wp:attachment":[{"href":"https:\/\/hesmid.nl\/test\/wp-json\/wp\/v2\/media?parent=14683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hesmid.nl\/test\/wp-json\/wp\/v2\/categories?post=14683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hesmid.nl\/test\/wp-json\/wp\/v2\/tags?post=14683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}