$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>woocommerce offtopic - search customers in front end by billing data|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

woocommerce offtopic - search customers in front end by billing data

matteradmin8PV0评论

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??

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??

Share Improve this question asked Mar 1, 2019 at 15:27 fakasmile1fakasmile1 12 bronze badges 1
  • 1 Why are you adding * to the search term? – Jacob Peattie Commented Mar 1, 2019 at 15:52
Add a comment  | 

1 Answer 1

Reset to default 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'
            )
        )
    );
Post a comment

comment list (0)

  1. No comments so far