对WP_LIST_TABLE进行排序时,表会进行排序,但我也会收到SQL错误

时间:2013-08-13 作者:dallen

这是我的宝贝prepare_items() 功能:

function prepare_items()
{
    global $wpdb;

    $table_name = $wpdb->prefix . \'blah\';
    $per_page = 100;
    $columns = $this->get_columns();
    $hidden = array();
    $sortable = $this->get_sortable_columns();

    $this->_column_headers = array($columns, $hidden, $sortable);

    $this->process_bulk_action();

    $total_items = $wpdb->get_var("SELECT COUNT(id) FROM $table_name");

    $paged = isset($_REQUEST[\'paged\']) ? max(0, intval($_REQUEST[\'paged\']) - 1) : 0;
    $orderby = (isset($_REQUEST[\'orderby\']) && in_array($_REQUEST[\'orderby\'], array_keys($this->get_sortable_columns()))) ? $_REQUEST[\'orderby\'] : \'title\';
    $order = (isset($_REQUEST[\'order\']) && in_array($_REQUEST[\'order\'], array(\'asc\', \'desc\'))) ? $_REQUEST[\'order\'] : \'asc\';

    $this->items = $wpdb->get_results($wpdb->prepare("SELECT * FROM $table_name ORDER BY $orderby $order LIMIT %d OFFSET %d", $per_page, $paged), ARRAY_A);

    $this->set_pagination_args(array(
        \'total_items\'   => $total_items,
        \'per_page\'      => $per_page,
        \'total_pages\'   => ceil($total_items / $per_page),
    ));
}
现在,当我对表排序时,URL变成:

http://blah.dev/wp-admin/admin.php?page=mvc_events&orderby=start_date&order=asc

[13-Aug-2013 20:55:21 UTC] WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'asc LIMIT 0, 10\' at line 1 for query SELECT `Event`.* FROM `wp_atb_events` `Event`    ORDER BY asc LIMIT 0, 10 made by do_action(\'toplevel_page_mvc_events\'), call_user_func_array, MvcDispatcher->admin_events_index, MvcDispatcher->__call, __lambda_func, MvcDispatcher::dispatch, AdminEventsController->index, MvcAdminController->set_objects, MvcModel->paginate, MvcDatabaseAdapter->get_results, MvcDatabase->get_results
[13-Aug-2013 20:55:21 UTC] WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'asc\' at line 1 for query SELECT COUNT(*) AS count FROM `wp_atb_events` `Event`    ORDER BY asc made by do_action(\'toplevel_page_mvc_events\'), call_user_func_array, MvcDispatcher->admin_events_index, MvcDispatcher->__call, __lambda_func, MvcDispatcher::dispatch, AdminEventsController->index, MvcAdminController->set_objects, MvcModel->paginate, MvcModel->get_total_count, MvcDatabaseAdapter->get_var, MvcDatabase->get_var
知道为什么会出现这个错误吗?一切正常,只是这两条错误信息不断出现。

1 个回复
SO网友:Puggan Se

零件ORDER BY $orderby $order LIMIT 将转换为ORDER BY asc LIMIT, 缺少supose为$orderby的列名,因此sql失败。

看起来您希望默认值为“title”

$orderby = (isset($_REQUEST[\'orderby\']) && in_array($_REQUEST[\'orderby\'], array_keys($this->get_sortable_columns()))) ? $_REQUEST[\'orderby\'] : \'title\';
may geuss是in\\u array()返回true,即使$\\u请求[\'orderby\']为空,因此接受空的$\\u请求[\'orderby\']而不是“title”。

如果添加此行,是否会显示错误消息?(低于$orderby, 在上面$this->items)

$orderby = $orderby :? \'title\';

结束

相关推荐

SQL:选择带有给定文本字符串的WordPress帖子并为其添加自定义域?

我想选择所有有特定文本字符串的wordpress帖子。然后向所有这些帖子添加自定义字段。这是我到目前为止得出的结论,但它不起作用INSERT INTO wp_postmeta (post_id, meta_key, meta_value) SELECT ID AS post_id, \'customer_reviews\' AS meta_key, \'On_or_delete\' AS meta_value FROM wp_posts