【问题标题】:Wordpress Custom database table with DataTable server side processing带有 DataTable 服务器端处理的 Wordpress 自定义数据库表
【发布时间】:2023-03-17 13:10:01
【问题描述】:

我已关注this toturial,并设法创建了一个短代码来显示带有过滤选项的数据表。此表适用于 wordpress 的原生帖子类型。

我需要相同的数据表,但它应该从我添加的自定义数据库表中获取数据wp_lubuvna_subscribers

自定义表:

所以我需要更改以下函数以从上述自定义表中获取数据,而不是自定义帖子类型

function datatables_server_side_callback_subscriber_db() {
 
    header("Content-Type: application/json");
 
    $request= $_GET;
 
    $columns = array(
        0   => 'post_title',
        3   => LUBUVNA_PREFIX.'email',
        1   => LUBUVNA_PREFIX.'first_name',
        2   => LUBUVNA_PREFIX.'last_name',
        4   => LUBUVNA_PREFIX.'phone'
        
    );
 
 
    $args = array(
        'post_type' => 'lubuvna_subscriber',
        'post_status' => 'publish',
        'posts_per_page' => $request['length'],
        'offset' => $request['start'],
        'order' => $request['order'][0]['dir'],
    );
 
    if ($request['order'][0]['column'] == 0) {
 
        $args['orderby'] = $columns[$request['order'][0]['column']];
 
    } elseif ($request['order'][0]['column'] == 1 || $request['order'][0]['column'] == 2) {
 
        $args['orderby'] = 'meta_value_num';
 
        $args['meta_key'] = $columns[$request['order'][0]['column']];
    }
 
    //$request['search']['value'] <= Value from search
 
    if( !empty($request['search']['value']) ) { // When datatables search is used
        $args['meta_query'] = array(
            'relation' => 'OR',
            array(
                'key' => 'd_title',
                'value' => sanitize_text_field($request['search']['value']),
                'compare' => 'LIKE'
            ),
            array(
                'key' => LUBUVNA_PREFIX.'email',
                'value' => sanitize_text_field($request['search']['value']),
                'compare' => 'LIKE'
            ),
            array(
                'key' => LUBUVNA_PREFIX.'first_name',
                'value' => sanitize_text_field($request['search']['value']),
                'compare' => 'LIKE'
            ),
            array(
                'key' => LUBUVNA_PREFIX.'last_name',
                'value' => sanitize_text_field($request['search']['value']),
                'compare' => 'LIKE'
            ),
            array(
                'key' => LUBUVNA_PREFIX.'phone',
                'value' => sanitize_text_field($request['search']['value']),
                'compare' => 'LIKE'
            ),
        );
    }
        
    $subscriber_query = new WP_Query($args);
    $totalData = $subscriber_query->found_posts;
 
    if ( $subscriber_query->have_posts() ) {
        
        while ( $subscriber_query->have_posts() ) {
         
            $subscriber_query->the_post();
 
            $nestedData = array();
            $nestedData[] = get_field(LUBUVNA_PREFIX.'first_name'). '&nbsp'. get_field(LUBUVNA_PREFIX.'last_name');
            $nestedData[] = "<a data-val='".get_post_field( 'post_name', get_post() )."' href='".get_permalink()."' data-posttype='".get_post_type( get_the_ID() )."' class='generallink'>" . get_field(LUBUVNA_PREFIX.'email') . "</a>";
            $nestedData[] = get_field(LUBUVNA_PREFIX.'first_name');
            $nestedData[] = get_field(LUBUVNA_PREFIX.'last_name');
            $nestedData[] = get_field(LUBUVNA_PREFIX.'phone');
 
            $data[] = $nestedData;
 
        }
 
        wp_reset_query();
 
        $json_data = array(
            "draw" => intval($request['draw']),
            "recordsTotal" => intval($totalData),
            "recordsFiltered" => intval($totalData),
            "data" => $data
        );
 
        echo json_encode($json_data);
 
    } else {
 
        $json_data = array(
            "data" => array()
        );
 
        echo json_encode($json_data);
    }
     
    wp_die();
 
}

【问题讨论】:

    标签: php mysql wordpress datatable


    【解决方案1】:

    所以我会在这里发布这个,以防其他人正在寻找相同的解决方案。我必须添加global $wpdb 并从我的自定义数据库表中获取数据。它与分页/排序/搜索完美配合。

    function datatables_server_side_callback_subscriber_db() {
     
        header("Content-Type: application/json");
    
        global $wpdb;
    
        // Table name
        $table_name = $wpdb->prefix . "lubuvna_subscribers";
    
        // Request
        $request= $_GET;
    
        // Columns
        $columns = array(
            0   => 'ID',
            1   => 'first_name',
            2   => 'last_name',
            3   => 'email',
            4   => 'phone'
            
        );
    
        // Datatable Filters
        $column = $columns[$request['order'][0]['column']];
        $offset = $request['start'];
        $length = $request['length'];
        $order = $request['order'][0]['dir'];
        
        // Search all columns
        $sql_where = "";
        if ( !empty($request['search']['value']) ) {
    
            $sql_where .= "WHERE ";
    
            foreach ($columns as $column) {
    
                 $sql_where .= $column . " LIKE '%" . sanitize_text_field($request['search']['value']) . "%' OR ";
    
            }
    
            $sql_where = substr($sql_where, 0, -3);
        }
    
        // Total Records in the datatable
        $total_table_records = "SELECT count(*) as count FROM {$table_name}";
        $total_fetched_records = $wpdb->get_results($total_table_records, OBJECT);
        $total_records = $total_fetched_records[0]->count;
    
        // Total Records Search
        $total_table_records_search = "SELECT count(*) as count FROM $table_name $sql_where";
        $total_fetched_records_search = $wpdb->get_results($total_table_records_search, OBJECT);
        $total_records_search = $total_fetched_records_search[0]->count;
    
        // Query
        $total_results = $wpdb->get_results( "SELECT * FROM $table_name $sql_where ORDER BY $column $order LIMIT $offset, $length" );
    
        if ( !empty($total_results) ) {
            foreach ($total_results as $row){
                $nestedData = array();
                $nestedData[] = $row->ID;
                $nestedData[] = $row->first_name;
                $nestedData[] = $row->last_name;
                $nestedData[] = $row->email;
                $nestedData[] = $row->phone;
                $data[] = $nestedData;
            }
    
            $json_data = array(
                "draw" => intval($request['draw']),
                "recordsTotal" => intval($total_records),
                "recordsFiltered" => intval($total_records_search),
                "data" => $data
            );
        
            echo json_encode($json_data);
    
        } else {
    
            $json_data = array(
                "data" => array()
            );
     
            echo json_encode($json_data);
    
        }
        
        wp_die();
     
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-23
      • 2016-02-26
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      相关资源
      最近更新 更多