0

i have got this code , but i can´t search by billing_phone

<form role="search" method="get" id="searchform" class="searchform" action="<?php echo site_url('/'); ?>">
    <div class="user-form">
        <label class="screen-reader-text" for="s">Search for:</label>
        <input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" />
        <input type="submit" id="searchsubmit" value="Search" />
    </div>
</form> .author-search -->

<?php
if( $_GET['s'] ) {

    $search_term = sanitize_text_field( stripslashes( $_GET['s']));


// WP_User_Query arguments
$args = array (
    'role'       => 'Customer',
    'order'      => 'ASC',
    'orderby'    => 'display_name',
    'search' => '*' . esc_attr( $search_term ) . '*',
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key'     => 'nickname',
            'value'   => $search_term,
            'compare' => 'LIKE'
        ),
        array(
            'key'     => 'last_name',
            'value'   => $search_term,
            'compare' => 'LIKE'
        ),
        array(
            'key'     => 'billing_phone',
            'value'   => $search_term,
            'compare' => 'LIKE'
        )
    )
);

// Create the WP_User_Query object
$wp_user_query = new WP_User_Query( $args );

// Get the results
$authors = $wp_user_query->get_results();

// Check for results
if ( ! empty( $authors ) ) {
    echo '<ul>';
    // loop through each author
    foreach ( $authors as $author ) {
        // get all the user's data
        $author_info = get_userdata( $author->ID );
        echo '<li>' . $author_info->nickname . ' ' . $author_info->last_name . '</li>';
                echo '<li>' . $author_info->billing_phone . '</li><br>';

    }
    echo '</ul>';
} 

else {
    echo 'No authors found';
}

}
?>

how do this??

  • 1
    Why are you adding * to the search term? – Jacob Peattie Mar 1 at 15:52
3

I think you have two issues that can be resolved in one go:

'search' => '*' . esc_attr( $search_term ) . '*',
  • You don't need the '*'s around the term. It looks like you're trying to do a wildcard, but that's what search already does by adding SQL '%' around your term. Essentially, you end up searching for "%*{$search_term}*%", which is probably not what you want.
  • esc_attr should be unnecessary here. The WordPress database class used in your query will escape your input for you.
  • Using search in this context is creating a query like this:
SELECT ... FROM wp_users JOIN wp_usermeta ... WHERE (
 (
   meta_key = 'nickname' AND meta_value LIKE 'search term'
 ) OR 
 (
   meta_key LIKE 'last_name'
 ...
 )
AND
 (
   wp_users.ID LIKE 'search term'
  OR
   wp_users.user_login LIKE 'search term'
 )
) -- END WHERE

which is also probably not what you want. In a WP_User_Query, search means "Search for user logins matching this query", not "search for general user meta matching this query."

So, try something like this:

    // wp_user_query arguments
    $args = array (
        'role'       => 'customer',
        'order'      => 'asc',
        'orderby'    => 'display_name',
        'meta_query' => array(
            'relation' => 'or',
            array(
                'key'     => 'nickname',
                'value'   => $search_term,
                'compare' => 'like'
            ),
            array(
                'key'     => 'last_name',
                'value'   => $search_term,
                'compare' => 'like'
            ),
            array(
                'key'     => 'billing_phone',
                'value'   => $search_term,
                'compare' => 'like'
            )
        )
    );
  • I am very grateful, it works correctly but I can not give you a public score for not having 15 points on my profile. I would like the link to appear in the result so that the customer profile can be edited if ( ! empty( $authors ) ) { echo '<ul>'; // loop through each author foreach ( $authors as $author ) { // get all the user's data $author_info = get_userdata( $author->ID ); echo '<li>' . $author_info->nickname . ' ' . $author_info->last_name . '</li>'; echo '<li>' . $author_info->billing_phone . '</li><br>'; } – fakasmile1 Mar 1 at 18:03
  • You should be able to accept the answer by clicking the "check" mark. Also, your loop code looks ok to me, but as a note you'll get WP_User objects back from your query, so calling get_userdata may be overkill. – phatskat Mar 1 at 18:52

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.