'tag.htm'; break; case 'flag': $pre .= $default_pre .= 'flag.htm'; break; case 'my': $pre .= $default_pre .= 'my.htm'; break; case 'my_password': $pre .= $default_pre .= 'my_password.htm'; break; case 'my_bind': $pre .= $default_pre .= 'my_bind.htm'; break; case 'my_avatar': $pre .= $default_pre .= 'my_avatar.htm'; break; case 'home_article': $pre .= $default_pre .= 'home_article.htm'; break; case 'home_comment': $pre .= $default_pre .= 'home_comment.htm'; break; case 'user': $pre .= $default_pre .= 'user.htm'; break; case 'user_login': $pre .= $default_pre .= 'user_login.htm'; break; case 'user_create': $pre .= $default_pre .= 'user_create.htm'; break; case 'user_resetpw': $pre .= $default_pre .= 'user_resetpw.htm'; break; case 'user_resetpw_complete': $pre .= $default_pre .= 'user_resetpw_complete.htm'; break; case 'user_comment': $pre .= $default_pre .= 'user_comment.htm'; break; case 'single_page': $pre .= $default_pre .= 'single_page.htm'; break; case 'search': $pre .= $default_pre .= 'search.htm'; break; case 'operate_sticky': $pre .= $default_pre .= 'operate_sticky.htm'; break; case 'operate_close': $pre .= $default_pre .= 'operate_close.htm'; break; case 'operate_delete': $pre .= $default_pre .= 'operate_delete.htm'; break; case 'operate_move': $pre .= $default_pre .= 'operate_move.htm'; break; case '404': $pre .= $default_pre .= '404.htm'; break; case 'read_404': $pre .= $default_pre .= 'read_404.htm'; break; case 'list_404': $pre .= $default_pre .= 'list_404.htm'; break; default: $pre .= $default_pre .= theme_mode_pre(); break; } if ($config['theme']) { $conffile = APP_PATH . 'view/template/' . $config['theme'] . '/conf.json'; $json = is_file($conffile) ? xn_json_decode(file_get_contents($conffile)) : array(); } !empty($json['installed']) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . ($id ? $id . '_' : '') . $pre; (empty($path_file) || !is_file($path_file)) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . $pre; if (!empty($config['theme_child']) && is_array($config['theme_child'])) { foreach ($config['theme_child'] as $theme) { if (empty($theme) || is_array($theme)) continue; $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . ($id ? $id . '_' : '') . $pre; !is_file($path_file) and $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . $pre; } } !is_file($path_file) and $path_file = APP_PATH . ($dir ? 'plugin/' . $dir . '/view/htm/' : 'view/htm/') . $default_pre; return $path_file; } function theme_mode_pre($type = 0) { global $config; $mode = $config['setting']['website_mode']; $pre = ''; if (1 == $mode) { $pre .= 2 == $type ? 'portal_category.htm' : 'portal.htm'; } elseif (2 == $mode) { $pre .= 2 == $type ? 'flat_category.htm' : 'flat.htm'; } else { $pre .= 2 == $type ? 'index_category.htm' : 'index.htm'; } return $pre; } ?>custom post types - WP_List_Table Inside Metabox Not Working on Clicking Search Button
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - WP_List_Table Inside Metabox Not Working on Clicking Search Button

programmeradmin24浏览0评论

I'm trying to display a WP_List_table inside a metabox. The metabox is for listing registered users to a course(LifterLMS Course Post Type). The table columns contain user_id, user_name, percentage & grades. I don't think the fact that I'm trying to this for a custom post type. I only merely mention to for clarity sake.

What's happening is that it all looks fine until I click the Search By Id( used wp search button function ) button on the custom post type edit screen. WP_List_Table will redirect back to the /wp-admin/edit.php page on clicking the search button ( after entering ID in input element). And it shows an admin notice of post updated on top of edit screen. What it should do , is to list only that user for which admin has searched. Same code i used on other admin menu pages worked fine. All other functions are working fine ( Ascending order, Descending order, order_by ). Only issue arises with search functionality. Code is attached below. data

/** 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 ) );
}

}

function list_table_layout() {

$myRequestTable = new List_Table_Class();

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_table_layout();

I'm trying to display a WP_List_table inside a metabox. The metabox is for listing registered users to a course(LifterLMS Course Post Type). The table columns contain user_id, user_name, percentage & grades. I don't think the fact that I'm trying to this for a custom post type. I only merely mention to for clarity sake.

What's happening is that it all looks fine until I click the Search By Id( used wp search button function ) button on the custom post type edit screen. WP_List_Table will redirect back to the /wp-admin/edit.php page on clicking the search button ( after entering ID in input element). And it shows an admin notice of post updated on top of edit screen. What it should do , is to list only that user for which admin has searched. Same code i used on other admin menu pages worked fine. All other functions are working fine ( Ascending order, Descending order, order_by ). Only issue arises with search functionality. Code is attached below. data

/** 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 ) );
}

}

function list_table_layout() {

$myRequestTable = new List_Table_Class();

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_table_layout();

Share Improve this question edited Oct 16, 2019 at 20:06 Muhammad Faizan Haidar asked Oct 16, 2019 at 19:26 Muhammad Faizan HaidarMuhammad Faizan Haidar 35 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Just got the solution. The issue was, while clicking on search submit button I got redirected to the same post with a post_updated message what happens here is basically post_update hook fires which redirects to the same post and I can't get search query_args. What I did is, I captured the update and post save hook and added query_args and then redirected to the respective post and hence it worked. WP List table search box input has name "s" so we have to look for "s"( $_GET["s"], $_POST["s"] ) Find the code below please:

 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;
        }
    }
发布评论

评论列表(0)

  1. 暂无评论