I’m utilizing autocomplete code as follows:
features.php
// add the ajax fetch js
add_action( 'wp_footer', 'ajax_fetch' );
perform ajax_fetch() {
?>
<script sort="text/javascript">
perform fetch(){
jQuery.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
sort: 'submit',
knowledge: { motion: 'data_fetch', key phrase: jQuery('#key phrase').val() },
success: perform(knowledge) {
jQuery('#datafetch').html( knowledge );
}
});
}
</script>
<?php }
// the ajax perform
add_action('wp_ajax_data_fetch' , 'data_fetch');
add_action('wp_ajax_nopriv_data_fetch','data_fetch');
perform data_fetch(){
$the_query = new WP_Query(
array(
'posts_per_page' => -1,
's' => esc_attr( $_POST['keyword'] ),
'post_type' => 'submit',
),
);
if( $the_query->have_posts() ) :
?><ul><?php
whereas( $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 category="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();
}
Web page template.php
<enter sort="text" identify="keyword" id="keyword" onkeyup="fetch()"></enter>
<div id="datafetch">Search outcomes will seem right here</div>
This works nice for autocomplete looking out of posts, however I’m having nice issue permitting it to look by creator identify (with role->creator) as an alternative of, or in addition to submit title.
I’ve tried to make use of a WP_User_Query instead of the WP_Query like so:
$args = array (
'function' => 'creator',
's' => esc_attr( $_POST['keyword'] ),
);
$user_query = new WP_User_Query( $args );
if ( ! empty( $user_query->outcomes ) ) {
foreach ( $user_query->outcomes as $consumer ) {
echo '<li><span>' . esc_html( $user->display_name ) . '</span></li>';
}
}
die();
}
This simply brings up the listing of each creator. Is there a technique to run a WP_User_Query with a whereas( $the_query->have_posts() ): $the_query->the_post();
as an alternative of a foreach ( $user_query->outcomes as $consumer ) {
? Or is there another technique to permit the search to deliver up an autocompleted listing of posts by means of a username search? I can not determine how.
Thanks