根据计费数据在前端搜索客户

时间:2019-03-01 作者:fakasmile1

我已获得此代码,但无法通过billing\\u 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\';
}

}
?>
如何做到这一点??

1 个回复
SO网友:phatskat

我认为您有两个问题可以一次性解决:

\'search\' => \'*\' . esc_attr( $search_term ) . \'*\',
<你不需要\'*\'学期结束了。看起来您正在尝试使用通配符,但这正是搜索通过在术语周围添加SQL“%”所做的。本质上,您最终会搜索"%*{$search_term}*%", 这可能不是你想要的
  • esc_attr 这里应该没有必要。查询中使用的WordPress数据库类将为您转义输入search 在这种情况下,创建这样的查询:
  • 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
    
    这也可能不是你想要的。在a中WP_User_Query, search 表示“搜索与此查询匹配的用户登录”,而不是“搜索与此查询匹配的一般用户元”

    那么,试试这样:

        // 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\'
                )
            )
        );
    

    相关推荐

    必需:在文件search.php中找到Get_BloInfo(‘模板_url’)。改用GET_TEMPLATE_DIRECTORY_URI

    主题检查错误→REQUIRED: get_bloginfo(\'template_url\') was found in the file search.php. Use get_template_directory_uri() instead. Line 21: <img src=\'<?php echo get_bloginfo(\'template_url\') ?>/img/no_results_found.png\'/> 我采取了行动并进行了更改,但该图