admin管理员组

文章数量:1312764

in short i want to search users with with help of live ajax search . i have written a code and it is working fine but that code is for searching posts by ajax, but i want to search users from wp_users table or wp_usersmeta table here below is my code input box

    <div class="container">
    <input type="text" name="keyword" id="keyword" onkeyup="fetch()"></input>
    <div id="datafetch">Search results will appear here</div>
    </div>

MY ajax code

    <?php
// add the ajax fetch js
add_action( 'wp_footer', 'ajax_fetch' );
function ajax_fetch() {
?>
<script type="text/javascript">
function fetch(){

    jQuery.ajax({
        url: '<?php echo admin_url('admin-ajax.php'); ?>',
        type: 'post',
        data: { action: 'data_fetch', keyword: jQuery('#keyword').val() },
        success: function(data) {
            jQuery('#datafetch').html( data );
        }
    });

}
</script>

<?php
} ?>

another post code

// the ajax function
add_action('wp_ajax_data_fetch' , 'data_fetch');
add_action('wp_ajax_nopriv_data_fetch','data_fetch');
function data_fetch(){

 $the_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 
    'posts_per_page' => 100,
        'post_type' => 'resume',
     ) );

   // print_r($the_query); echo "jaani";
    if( $the_query->have_posts() ) {
        while( $the_query->have_posts() ): $the_query->the_post(); ?>

            <h2><a href="<?php echo esc_url( post_permalink() ); ?>"><?php the_title();?></a></h2>

        <?php endwhile;
        wp_reset_postdata();  
    } else{
        echo"No Post Found";
    }

    die(); 
}


?>

本文标签: plugin developmenthow to search users by ajax live search