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

Search not working fine

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;
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.