php - 从 WP_List_Table 代码处理 Wordpress 中的搜索

标签 php wordpress

我需要添加对使用 WP_List_Table 搜索或过滤项目的支持 我读过但不知道如何完成这项工作。我已经通过这段代码添加了盒子:

function ft_list()
{
    echo '</pre><div class="wrap"><h2>' . __('Frequent Traveler List') . '</h2>';
    $ftList = new FT_WP_Table();
    $ftList->search_box('search', 'search_id');
    echo '</div>';
}

这就是我的 prepare_items() 函数的样子:

public function prepare_items()
{
    $searchcol = array(
        'firstname',
        'lastname',
        'foid',
        'no_frequent',
        'create_time'
    );

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

    // SQL results
    $posts = $this->get_sql_results();
    empty($posts) AND $posts = array();

    # >>>> Pagination
    $per_page = $this->posts_per_page;
    $current_page = $this->get_pagenum();
    $total_items = count($posts);
    $this->set_pagination_args(array(
        'total_items' => $total_items,
        'per_page' => $per_page,
        'total_pages' => ceil($total_items / $per_page)
    ));
    $last_post = $current_page * $per_page;
    $first_post = $last_post - $per_page + 1;
    $last_post > $total_items AND $last_post = $total_items;

    // Setup the range of keys/indizes that contain 
    // the posts on the currently displayed page(d).
    // Flip keys with values as the range outputs the range in the values.
    $range = array_flip(range($first_post - 1, $last_post - 1, 1));

    // Filter out the posts we're not displaying on the current page.
    $posts_array = array_intersect_key($posts, $range);
    # <<<< Pagination
    // Prepare the data
    $permalink = __('Edit:');

    foreach ($posts_array as $key => $post) {
        $link = get_edit_post_link($post->id);
        $no_title = __('No title set');
        $title = !$post->firstname ? "<em>{$no_title}</em>" : $post->firstname;
        $posts[$key]->firstname = "<a title='{$permalink} {$title}' href='{$link}'>{$title}</a>";
    }
    $this->items = $posts_array;
}

我应该在哪里添加代码以根据搜索参数过滤项目?

最佳答案

查看扩展 WP_List_Table 的其他类,我看到他们使用类似的东西

$search = ( isset( $_REQUEST['s'] ) ) ? $_REQUEST['s'] : false;

并将其传递给 prepare_items() 方法。

在我的测试插件中,我制作了这个(参见Percent in wpdb->prepare):

$search = ( isset( $_REQUEST['s'] ) ) ? $_REQUEST['s'] : false;
$do_search = ( $search ) ? $wpdb->prepare(" AND post_content LIKE '%%%s%%' ", $search ) : ''; 
$sql_results = $wpdb->get_results("
        SELECT $sql_select
            FROM $wpdb->posts
        WHERE post_status = 'publish'
        $do_search
        ORDER BY $this->orderby $this->order "
);

display_tablenav() 方法中我添加了:

<form action="" method="GET">
    <?php 
    $this->search_box( __( 'Search' ), 'search-box-id' ); 
    ?>
    <input type="hidden" name="page" value="<?= esc_attr($_REQUEST['page']) ?>"/>
</form>

我还没有创建任何 search_box() 方法,让父类渲染它。

关于php - 从 WP_List_Table 代码处理 Wordpress 中的搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24191197/

相关文章:

mysql - 将 SQL 数据库导入实时服务器,表丢失

php - 如何从非管理员用户的类别列表小部件中排除类别

PHP 在文件路径中转义\14 个字符

Arduino返回的PHP串口数据

PHP 警告 : php_network_getaddresses: getaddrinfo failed: Name or service not known in/some/path on line x

PHP wordpress 获取类别对象的多维数组

php - 使用 mysqli 进行简单的错误检查?

php - 使用 PHP 将换行符传递到文本区域

php - 为什么我有 WooCommerce 订单,但没有 shop_order 类型的帖子?

windows - 当您已经安装了 IIS/SQL Server 时,在 Windows 上针对 WordPress 进行开发的最佳方法是什么?