Problem saving Custom Post type showing wp_list_table class inside metabox

Question

Regards,
I’m trying to show my Custom Post Type called “Tasks” in the edit screen of another Custom Post Type called “Projects”, I show it as a table using the wp_list_table class and then I insert it in a METABOX that appears in the edit window of the “Projects”, following some tutorials I have been able to get it to be displayed in this way:
enter image description here

That table (TASKs) is only for reference, I do not want to do any editing or deletion.
I show that table (TASKs) in the editing window of the CPT “Projects”, the problem is that when I edit the “Project” for example the title I get an error
enter image description here

This is my code :

if ( ! class_exists( ‘WP_List_Table’ ) ) {

require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );

}

class Customers_Lista extends WP_List_Table {

/** Class constructor */
public function __construct() {

    parent::__construct( [
        'singular' => __( 'Customer', 'sp' ), //singular name of the listed records
        'plural'   => __( 'Customers', 'sp' ), //plural name of the listed records
        'ajax'     => false //does this table support ajax?
    ] );

}


/**
 * Retrieve customers data from the database
 *
 * @param int $per_page
 * @param int $page_number
 *
 * @return mixed
 */


public static function get_customers() {
  $query = new WP_Query(array(
      'post_type' => 'tareast',
         /*'meta_key'=>'fecha_de_la_tarea',
          
         'orderby'=>esc_sql( $_REQUEST['orderby'] ),
          'order'=>'ASC',
            'meta_query'=>array(
                array(
                'key'=> 'proyecto_relacionados',
                'compare'=>'LIKE',
                    'value'=>get_the_ID(),              
                    ),
                                )    */
  ));

  $customer_posts = $query->posts;
  $customers = array();

  foreach($customer_posts as $customer_post) {
    $new_customer = array();
    $new_customer['Id'] = $customer_post->ID;
    $new_customer['titulo'] = $customer_post->post_title;
    $new_customer['fecha_de_la_tarea'] = get_post_meta($customer_post->ID, 'fecha_de_la_tarea',true);

    array_push($customers, $new_customer);
  }

  return $customers;
}

/**
 * Delete a customer record.
 *
 * @param int $id customer ID
 */
public static function delete_customer( $id ) {
    global $wpdb;

    $wpdb->delete(
        "{$wpdb->prefix}customers",
        [ 'ID' => $id ],
        [ '%d' ]
    );
}


/**
 * Returns the count of records in the database.
 *
 * @return null|string
 */
public static function record_count() {
  return wp_count_posts('tareast')->publish;
}

/** Text displayed when no customer data is available */
public function no_items() {
    _e( 'No customers avaliable.', 'sp' );
}

 function column_default($customer, $column_name) {
  return $customer[$column_name];
}

 function get_columns() {
  return [
    'cb' => '<input type="checkbox" />',
    'Id' => __('Customer ID', 'text-domain'),
    //'name' => __('Tarea', 'text-domain'),
    'titulo' => __('Titulo', 'text-doamin'),
    'fecha_de_la_tarea' => __('Fecha', 'text-domain')
  ];
}


public function get_sortable_columns() {
  return [
    'Id' => ['Id', true],
    'titulo' => ['title', false],
      'fecha_de_la_tarea' =>['fecha_de_la_tarea', false]
  ];
}


public function prepare_items() {
    //$this->_column_headers = $this->get_column_info();
    $this->_column_headers = [
$this->get_columns(),
[], 
$this->get_sortable_columns(),

$this->get_primary_column_name(),
];
/** Process bulk action */

    $this->process_bulk_action();
    $per_page     = $this->get_items_per_page( 'customers_per_page',4 );
    $current_page = $this->get_pagenum();
    $total_items  = self::record_count();
    $this->set_pagination_args( [
        'total_items' => $total_items, 
        'per_page'    => $per_page 
    ] );
    $this->items = self::get_customers( $per_page, $current_page );
}

}

class SP_Plugina {

// class instance
static $instance;

// customer WP_List_Table object
public $customers_obj;

public $customers_objs;
// class constructor
public function __construct() {
add_filter( ‘set-screen-option’, [ CLASS, ‘set_screen’ ], 10, 3 );
add_action( ‘admin_menu’, [ $this, ‘plugin_menu’ ] );
}

public static function set_screen( $status, $option, $value ) {
    return $value;
}

public function plugin_menu() {

    $hook = add_menu_page(
        'Sitepoint WP_List_Table Example',
        'SP WP_List_Table',
        'manage_options',
        'wp_list_table_class',
        [ $this, 'plugin_settings_page' ]
    );

    add_action( "load-$hook", [ $this, 'screen_option' ] );

}


/**
 * Plugin settings page
 */
public function plugin_settings_page() {
    ?>
    <div class="wrap">
        <h2>WP_List_Table Class Example</h2>

        <div id="poststuff">
            <div id="post-body" class="metabox-holder columns-2">
                <div id="post-body-content">
                    <div class="meta-box-sortables ui-sortable">
                        <form method="post">
                            <?php
                            $this->customers_obj->prepare_items();
                            $this->customers_obj->display(); ?>
                        </form>
                    </div>
                </div>
            </div>
            <br class="clear">
        </div>
    </div>
<?php
}


/**
 * Screen options
 */

public function screen_option() {

    $option = 'per_page';
    $args   = [
        'label'   => 'Customers',
        'default' => 4,
        'option'  => 'customers_per_page'
    ];

    add_screen_option( $option, $args );

    $this->customers_obj = new Customers_Lista();
}

/** Singleton instance */
public static function get_instance() {
    if ( ! isset( self::$instance ) ) {
        self::$instance = new self();
    }

    return self::$instance;
}

}

add_action( ‘plugins_loaded’, function () {
SP_Plugina::get_instance();

} );

And this is the code that I use to put the table in a metabox.

function servicesMetaBox() {

$servicesTable = new Customers_Lista();
$servicesTable->prepare_items();
$servicesTable->display(); 

}

function changeServicesMetaBoxPosition() {

global $wp_meta_boxes;

add_meta_box('tagsdiv-service',__('Services'),'servicesMetaBox','proyectost','normal','low');

}

add_action(‘add_meta_boxes’,’changeServicesMetaBoxPosition’,0);

—–Then I publish the metabox in the custom post type Projects.

However I am stuck in the problem of not being able to save the project, please if someone has experience with wp_list_table or can tell me what I am doing wrong.
Regards.

0
, Esteban Sanchez 3 months 0 Answers 51 views 0

Leave an answer