How to get WordPress post title in text field - autocomplete

Thursday, December 03, 2015

 Here is just simple code by which you can get wordpress posts in text box in autocomplete. [like search]




<?php
      // The Query
$the_query = new WP_Query( 'post_type=post' ); //Whatever your posts type.

// The Loop
if ( $the_query->have_posts() ) {
  foreach ($the_query->posts as $post_nm) {
    if($post_nm->post_title != ''){
      $post_names[] =  $post_nm->post_title;
    }
  }
}
/* Restore original Post Data */
wp_reset_postdata();

$post_names = implode('","', $post_names);
?>
<script type="text/javascript">
$(document).ready(function(){
    
    var availableTags = ["<?php echo $post_names; ?>"];  
    //console.log(availableTags);
    $('#textFeild').keyup(function(){  //Enter your text field id
         $('#textFeild').autocomplete({
             minLength: 1,
            source: availableTags
        });
    });
});
</script>
       
thanks :)

Bilal Hussain | Get post’s arguments on text input – autocomplete | wordpress

You Might Also Like

0 comments

Popular Posts

Subscribe