ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php – 从WP_List_Table代码处理WordPress中的搜索

2019-08-25 03:30:09  阅读:388  来源: 互联网

标签:php wordpress wordpress-plugin


我需要使用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,wordpress,wordpress-plugin
来源: https://codeday.me/bug/20190825/1715017.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有