I am trying to devlope a plugin that will search and return my users so I can edit some other metatdata that belongs to them. I have been using the plug-in listed on the Codex as my starting point.
I have my table appearing with the the users from a WP_User_Query. But I am having issues with filtering the query with the search input.
Wildcard search mysearch will return the table correctly, but going to the second page returns back to a blank search. The search variable is not being placed in the url. How do I pass this to the url? (the pagination is passed only)
Also is there a way to change it so that I do not have to physically at “*” before and after the search parameters and have it done in the code?
Here is my query:
function prepare_items() {
global $wpdb;
$searchcol= array(
'ID',
'user_email',
'user_login',
'user_nicename',
'user_url',
'display_name'
);
$orderby = !empty($_GET["orderby"]) ? mysql_real_escape_string($_GET["orderby"]) : 'email';
$order = !empty($_GET["order"]) ? mysql_real_escape_string($_GET["order"]) : 'ASC';
$args = array(
'fields' => 'all_with_meta',
'orderby' => $orderby ,
'order' => $order ,
'search' =>$_REQUEST["s"] ,
'search_columns' => $searchcol
);
$my_query = new WP_User_Query( $args );
}
and here is my search field:
function my_render_list_table_page(){
global $my_list_table_sample_page, $wpdb;
$my_list_table_sample_page->prepare_items();
<form action="" method="post" >
<?php
$my_list_table_sample_page->search_box( __( 'Search Users' ), 'user' );
$my_list_table_sample_page->display();
$my_list_table_sample_page->display();
echo '</form>';
}
Brian is correct. By changing the form to use get instead of post and then using $_REQUEST to fetch it should work, and is for me.
As Brian noted, your form sends the data via POST, and you are fetching via GET. Change for method to “GET” or else use the $_POST variable. (But note that your search term is passed along using your current code, because both POST and GET populate the $_REQUEST variable).
You can make the search more flexible by ensuring that it is surrounded by wildcard (asterisk) characters. Something like this should do the trick:
$search = preg_replace( "^$**(.*)**$?#", "*$1*", $_REQUEST["s"] );
This will allow the search input was submitted with zero or more leading/trailing asterisks, and ensure that your search string is preceded and followed by a single asterisk.