Metabox中的WP_LIST_TABLE在单击搜索按钮时不起作用

时间:2019-10-16 作者:Muhammad Faizan Haidar

我试图在元框中显示WP\\U List\\U表。metabox用于列出课程的注册用户(StreetLMS课程帖子类型)。表格列包含user\\u id、user\\u name、percentage&;等级。我不认为这是一个事实,我正试图为一个自定义的帖子类型。我只是为了清楚起见才提到。

现在的情况是,在我单击自定义帖子类型编辑屏幕上的“按Id搜索”(使用的wp搜索按钮功能)按钮之前,一切看起来都很好。WP\\u List\\u表将重定向回/WP admin/edit。php页面上单击搜索按钮(在输入元素中输入ID后)。它会在编辑屏幕的顶部显示更新后的帖子管理通知。它应该做的是只列出管理员搜索过的用户。我在其他管理菜单页面上使用的相同代码运行良好。所有其他功能都工作正常(升序、降序、order\\u by)。只有搜索功能出现问题。代码附在下面。数据

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

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

}

/**
 * Function to filter data based on order , order_by & searched items
 *
 * @param string $orderby
 * @param string $order
 * @param string $search_term
 * @return array $users_array()
 */
public function list_table_data_fun( $orderby=\'\', $order=\'\' , $search_term=\'\' ) {

    $users_array = array();
    $args        = array();
    if( !empty( $search_term ) ) {
        $searchcol= array(
            \'ID\',
            \'user_email\',
            \'user_login\',
            \'user_nicename\',
            \'user_url\',
            \'display_name\'
        );

        $args  = array(
            \'fields\'         => \'all_with_meta\', 
            \'orderby\'        => $orderby , 
            \'order\'          => $order , 
            \'search\'         => $_REQUEST["s"] ,
            \'search_columns\' => $searchcol
            );
        } else {
                if( $order == "asc" && $orderby == "id" ) {
                    $args = array(
                        \'orderby\'      => \'ID\',
                        \'order\'        => \'ASC\',
                    ); 
            } elseif ( $order == "desc" && $orderby == "id"  ) {
                    $args = array(
                        \'orderby\'      => \'ID\',
                        \'order\'        => \'DESC\',
                    );

            } elseif ( $order == "desc" && $orderby == "title"  ) {
                    $args = array(
                        \'orderby\'      => \'name\',
                        \'order\'        => \'DESC\',
                    );
            } elseif ( $order == "asc" && $orderby == "title"  ) {
                $args = array(
                    \'orderby\'      => \'name\',
                    \'order\'        => \'ASC\',
                );
            } else {
                $args = array(
                    \'orderby\'      => \'ID\',
                    \'order\'        => \'DESC\',
                );
            }

        }

        $users = get_users( $args );

        if( count( $users ) > 0 ) {
            foreach ( $users as $index => $user) {
                $author_info = get_userdata( $user->ID );
                if ( $user ) {

                    $users_array[] = array(
                        "id"         => $user->ID,
                        "title"      => \'<b><a href="\' .get_author_posts_url( $user->ID ). \'"> \'. $author_info->display_name .\'</a></b>\' ,
                        "percentage" => 10,
                        "grade"      => \'<label style="color:\'.\'black\'.\';"><b>\'. \'A\'.\'</b><label>\'
                    );
                }
                # code...
            }
        }

    return $users_array;
}
//prepare_items
public function prepare_items() {

    $orderby = isset( $_GET[\'orderby\'] ) ? trim( $_GET[\'orderby\'] ): "";
    $order   = isset( $_GET[\'order\'] ) ? trim( $_GET[\'order\'] ) : "";

    $search_term  = isset( $_POST[\'s\'] ) ? trim( $_POST[\'s\'] ) : "";

    if( $search_term == "" ) {

        $search_term  = isset( $_GET[\'s\'] ) ? trim( $_GET[\'s\'] ) : "";
    }

    $datas        = $this->list_table_data_fun( $orderby, $order, $search_term );


    $per_page     = 3;
    $current_page = $this->get_pagenum();
    $total_items  = count($datas);

    $this->set_pagination_args( array( "total_items"=> $total_items,
        "per_page" => $per_page ) );

    $this->items = array_slice( $datas, ( ( $current_page - 1 )* $per_page ), $per_page );

    $columns  = $this->get_columns();
    $hidden   = $this->get_hidden_columns();
    $sortable = $this->get_sortable_columns();

    $this->_column_headers = array( $columns, $hidden, $sortable );
}
    //get_columns
public function get_columns() {

    $columns = array(
        "cb"         => "<input type=\'checkbox\'/>",
        "id"         => "ID",
        "title"      => "User Name",
        "percentage" => "Percentage",
        "grade"      => "Grade",
        "action"     => "Action"        
    );

    return $columns;
}

public function get_hidden_columns() {
    return array("");
}

public function get_sortable_columns() {
        return array (
        "title" => array( "title", true ),
        "id"    => array( "id", true ),
    );

}   

//column_default
public function column_default( $item, $column_name ){
    switch ( $column_name ) {
        case \'id\':


        case \'title\':


        case \'percentage\':

        case \'grade\':
        return $item[ $column_name ];
        case \'action\':  
            if ( isset ( $_GET[\'page\'] ) ){
                $page = $_GET[\'page\'];
            } else {
                global $pagenow;
                $page = $pagenow;
            }
            return \'<a href="?page=\'.$page.\'&action=llmsgb-edit&user_id=\'.$item[\'id\'].\'">Edit</a>\';

        default:
            return "no value";

    }

}
protected function display_tablenav( $which ) {

    // REMOVED NONCE -- INTERFERING WITH SAVING POSTS ON METABOXES
    // Add better detection if this class is used on meta box or not.

    // if ( \'top\' == $which ) {
    //  wp_nonce_field( \'bulk-\' . $this->_args[\'plural\'] );
    // }

}

public function column_title( $item ) {
    if ( isset ( $_GET[\'page\'] ) ){
        $page = $_GET[\'page\'];
    } else {
        global $pagenow;
        $page = $pagenow;
    }
    $action = array(
            "edit" => sprintf(\'<a href="?page=%s&action=%s&user_id=%s">Edit</a>\',$page,\'llmsgb-edit\',$item[\'id\']));
    return sprintf(\'%1$s %2$s\', $item[\'title\'],$this->row_actions( $action ) );
}
}

功能列表\\u table\\u布局(){

$myRequestTable=新列表\\u表\\u类();

global $pagenow;
?>
<div class="wrap"><h2>Students Grades Information</h2>
<form method="get">
<input type="hidden" name="page" value="<?php echo $pagenow ?>" />
<?php if( isset( $myRequestTable ) ) : ?>
    <?php $myRequestTable->prepare_items();  ?>
    <?php $myRequestTable->search_box( __( \'Search Students By ID\' ), \'students\' ); //Needs To be called after $myRequestTable->prepare_items() ?>
    <?php $myRequestTable->display(); ?>
}list\\u table\\u layout();

Search not working fine

1 个回复
最合适的回答,由SO网友:Muhammad Faizan Haidar 整理而成

刚刚找到解决方案。问题是,当单击search submit按钮时,我被重定向到了同一篇文章,并显示了一条post\\u更新消息。这里发生的基本上是post\\u更新挂钩触发,它重定向到同一篇文章,我无法获取search query\\u参数。我所做的是,我捕获了update和post save挂钩,添加了query\\u args,然后重定向到相应的post,因此它起了作用。WP列表搜索框输入的名称为“s”,因此我们必须查找“s”($\\u GET[“s”],$\\u POST[“s”])请查找以下代码:

 add_action( \'save_post\',\'save_query_string\', 100, 3 );
 add_action( \'post_updated\',\'save_query_string\', 10, 3 );
 function save_query_string( $post_id, $post, $update ) {
        $post_type = get_post_type($post);
        $search_term  = isset( $_POST[\'s\'] ) ? trim( $_POST[\'s\'] ) : "";
        if ( $search_term == "" ) {
            $search_term  = isset( $_GET[\'s\'] ) ? trim( $_GET[\'s\'] ) : "";
        }
        if ( $post_type == \'product\' && $search_term != "" ) {
            wp_safe_redirect( add_query_arg( \'s\', $search_term, $_POST[\'_wp_http_referer\'] ) );
            exit;
        }
    }

相关推荐

Editing a Search Form

我正在我的Wordpress网站上制作一个搜索表单。它有一个高级搜索框,其中有一个高级按钮,单击该按钮可删除要包含的自定义过滤器。https://ibb.co/R7DFC1F >> 我想删除这个高级按钮,而下面的所有过滤器[类型、省、类别和价格]必须保持不变(不点击任何内容)。我的暂存网站位于http://f1stock.info/1 <div class=\"iw-field iw-display-table-cell field-for-add-advanced\">&#