Search code examples
wordpressadvanced-custom-fields

Can you edit WordPress custom post types directly from the archive list page?


Is there a way to edit WordPress custom post types directly from the archive list page. For example, you would use the "list" view to edit the posts and pages in that custom post type, including all o the custom fields. enter image description here


Solution

  • Option 1: Using a plugin

    If you're using ACF, the easiest way to do this would be with ACF Quick Edit Fields plugin.

    Option 2: Using code

    If you don't want to use a plugin, you need to:

    1. Add columns to the list page
    2. Get the fields and display it in these columns

    You can add the following code to your theme's functions.php:

    1. Add columns for the data you want displayed in the list
    // Replace 'cpt_name' with your custom post type name
    function add_custom_columns ( $columns ) {
      $columns['custom_field_1'] = __( 'Custom Field 1 Column Name' );
      $columns['custom_field_2'] = __( 'Custom Field 2 Column Name' );
      return $columns;
    }
    add_filter ( 'manage_cpt_name_posts_columns', 'add_custom_columns' );
    
    
    1. Get the fields and display it in the columns
    function display_cf_columns ( $column, $post_id ) {
       switch ( $column ) {
         case 'custom_field_1':       
           echo get_post_meta ( $post_id, 'post_meta_1', true );
           break;
         case 'custom_field_2':
           echo get_post_meta ( $post_id, 'post_meta_1', true );
           break;
       }
     }
     add_action ( 'manage_cpt_name_posts_custom_column', 'display_cf_columns', 10, 2 );