admin管理员组

文章数量:1287644

I am using autocomplete code as follows:

functions.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 }

// 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'] ),
        'post_type' => 'post',

        ),
    );

    if( $the_query->have_posts() ) :
        ?><ul><?php
        while( $the_query->have_posts() ): $the_query->the_post();

        $myquery = esc_attr( $_POST['keyword'] );
        $a = $myquery;
        $search = get_the_title();
        if( stripos("/{$search}/", $a) !== false) {?>
            <li><a class="articles-link" href="<?php echo esc_url( post_permalink() ); ?>"><?php the_title();?></a> by <?php the_author(); ?></li>
        <?php }
    endwhile;
        wp_reset_postdata();
        ?></ul><?php
    endif;
    die();
}

Page template.php

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

This works great for autocomplete searching of posts, but I am having great difficulty allowing it to search by author name (with role->author) instead of, or as well as post title.

I have tried to use a WP_User_Query in place of the WP_Query like so:

$args = array (
    'role'           => 'author',
    's' => esc_attr( $_POST['keyword'] ),
);
$user_query = new WP_User_Query( $args );
if ( ! empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) {
        echo '<li><span>' . esc_html( $user->display_name ) . '</span></li>';
    }
}
    die();
}

This just brings up the list of every author. Is there a way to run a WP_User_Query with a while( $the_query->have_posts() ): $the_query->the_post(); instead of a foreach ( $user_query->results as $user ) {? Or is there some other way to allow the search to bring up an autocompleted list of posts by way of a username search? I can't figure out how.

Thank you

I am using autocomplete code as follows:

functions.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 }

// 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'] ),
        'post_type' => 'post',

        ),
    );

    if( $the_query->have_posts() ) :
        ?><ul><?php
        while( $the_query->have_posts() ): $the_query->the_post();

        $myquery = esc_attr( $_POST['keyword'] );
        $a = $myquery;
        $search = get_the_title();
        if( stripos("/{$search}/", $a) !== false) {?>
            <li><a class="articles-link" href="<?php echo esc_url( post_permalink() ); ?>"><?php the_title();?></a> by <?php the_author(); ?></li>
        <?php }
    endwhile;
        wp_reset_postdata();
        ?></ul><?php
    endif;
    die();
}

Page template.php

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

This works great for autocomplete searching of posts, but I am having great difficulty allowing it to search by author name (with role->author) instead of, or as well as post title.

I have tried to use a WP_User_Query in place of the WP_Query like so:

$args = array (
    'role'           => 'author',
    's' => esc_attr( $_POST['keyword'] ),
);
$user_query = new WP_User_Query( $args );
if ( ! empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) {
        echo '<li><span>' . esc_html( $user->display_name ) . '</span></li>';
    }
}
    die();
}

This just brings up the list of every author. Is there a way to run a WP_User_Query with a while( $the_query->have_posts() ): $the_query->the_post(); instead of a foreach ( $user_query->results as $user ) {? Or is there some other way to allow the search to bring up an autocompleted list of posts by way of a username search? I can't figure out how.

Thank you

Share Improve this question edited Sep 17, 2021 at 21:51 mrmotivator asked Sep 17, 2021 at 21:36 mrmotivatormrmotivator 133 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The WP_User_Query uses the parameter search rather than s https://developer.wordpress/reference/classes/wp_user_query/#search-parameters

So try this instead

$args = array (
    'role'           => 'author',
    'search' => esc_attr( $_POST['keyword'] ),
);

本文标签: wp queryWordpress search posts by author name with autocomplete