Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>'; 
}
share|improve this question
    
Found the parameter 'search_columns' for WP_User_Query then created an array with 'ID', 'user_email', 'user_login', 'user_nicename', 'user_url', 'display_name' for its parameters. Now to add wildcard to the search as it only returns full matches. –  Ken Aug 2 '12 at 16:23
1  
The search parameter is not being placed in the url because your form is using POST instead of GET. –  Brian Jan 24 '13 at 8:46
add comment

1 Answer 1

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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