Wordpress最终成员编辑自定义字段的搜索过滤器

时间:2016-01-24 00:49:05

标签: wordpress

有没有办法在Wordpress插件最终成员上编辑搜索过滤器?据我所知,标准过滤器仅包括从后端选择的字段,搜索用户必须匹配所有指定的字段(逻辑和)。我想要一些方法来编辑seach并为某些字段添加更多功能(或者,少于,多于)。到目前为止,我还没有找到如何基于全局$ wpdb变量或其他方式提出搜索。

1 个答案:

答案 0 :(得分:0)

我在示例中使用Ajax根据条件搜索用户,在这里我使用国家/地区,已经在数组中定义了城市列表,在这里循环调用它。

<?php /*Template Name: Users* /?> 


<div class="row">
        <div class="col mb-2">
            <h5 class="mb-3" >
                <?php echo __('Advanced','booksinfo') ?>
            </h5>
            
            <input class="form-control mb-2" placeholder="<?php echo __('User name','booksinfo') ?>" name="username" id="username" onkeyup="users_fetch()"  />
                            
            <select name="usercountry" id="usercountry" class="form-control" onchange="users_fetch()">
                <option value="all" ><?php echo __('All countries','booksinfo') ?></option>
                <?php
                    $user_country = get_user_meta( $current_user->ID, 'user_registration_user_country', true );


                        foreach ($countrys_list as $country ) {
                            
                            echo "<option value=\"$country\">".esc_html__($country,'booksinfo') ."</option>";
                        
                    }
                    ?>
            </select>
            
        </div>
        <div class="col-md-9">
            <div class="row" id="users_list_div">
                <script>
                    jQuery.ajax({
                        url: '<?php echo admin_url('admin-ajax.php'); ?>',
                        type: 'post',
                        data: { action: 'users_data_fetch'},
                        success: function(data) {
                            jQuery('#users_list_div').html( data );
                        }
                    });
                </script>
            </div>
        </div>
        </div>

好吧,现在我们已经完成了一个表单,可以按名称和城市搜索用户,我们正在编写functions.php中的代码

// add the ajax fetch js
function users_ajax_fetch() {
?>
<script type="text/javascript">
function users_fetch(){
    jQuery.ajax({
        url: '<?php echo admin_url('admin-ajax.php'); ?>',
        type: 'post',
        data: { action: 'users_data_fetch', username: jQuery('#username').val(), usercountry: jQuery('#usercountry').val() },
        success: function(data) {
            jQuery('#users_list_div').html( data );
        }
    });

}
</script>

<?php
}
add_action( 'wp_footer', 'users_ajax_fetch' );

function users_data_fetch(){
    if( $_POST['username']){
        if($_POST['usercountry'] != 'all'){
            $user_country = $_POST['usercountry'];
            $compare_country = '=';
        }else{
            $user_country = '';
            $compare_country = 'LIKE';
        }
        
        $args = array(
                    'orderby' => 'first_name',
                    'order'   => 'ASC',
                    'meta_query'    => array(
                        array(
                        'key'      =>  'user_registration_user_country',
                        'value'    =>  $user_country,
                        'compare'      => $compare_country,
                        ),
                        array(
                        'key'      =>  'nickname',
                        'value'    =>  $_POST['username'],
                        'compare'      => 'LIKE',
                        )
                    )

                );
    //}
$users = get_users( $args );
$users_result = $users[0]->ID;
if($users_result){
foreach ( $users as $user ) {
$user_pic = get_user_meta( $user->ID, 'profilepicture', true );
$user_type = get_user_meta( $user->ID, 'user_registration_user_type', true );

?>
    <div class="col-xl-4 col-md-6 col-sm-6 text-center mb-3">
        <div class="col border pt-2">
            <a href="<?php echo get_site_url() .'/author/'. $user->user_nicename ?>" >
                <? if($user_pic){ ?>
                    <div class="" style="background:url('<?php echo $user_pic ?>');width:110px;height:110px;margin: auto;background-size: cover;background-position: center;    border-radius: 100px;" ></div>
                <?php
                }else{
                        echo '<img src="' . get_template_directory_uri() . '/images/user.svg" style="border-radius:100px;width:110px;height:110px;margin">';
                        //echo get_avatar( $curauth->user_email , '120 '); 
                }
            </a>
            <div class="mt-1 mb-3">
                <?php echo $user->display_name; ?>
            </div>
            <div class="row">
                <a class="col text-center btn btn-success" href="<?php echo get_site_url() .'/author/'. $user->user_nicename ?>" >
                    <?php echo __('View prfile','booksinfo') ?>
                </a>
            </div>
        </div>
    </div>
<?php 
}
}else{ ?>
<h5 class="text-center mx-auto mt-3">
    <?php echo __('What you are looking for is not currently found. Please check the case','booksinfo'); ?>
</h5>
<?php }
   }
    }
    add_action('wp_ajax_nopriv_users_data_fetch','users_data_fetch');
    add_action('wp_ajax_users_data_fetch' , 'users_data_fetch');

这是我发现的最佳方法,这是我给的示例页面 page link