WordPress search Post(news) with ajax

js code


/* All filter news and pagination*/
jQuery('.news-filter .categoryCheck').on( 'change' , function() {
if(!jQuery(this).val()){
jQuery('.news-filter .categoryCheck').prop( "checked", false );
}
load_news_list(0,1);
});

jQuery('.search-btn').on( 'click' , function() {
load_news_list(0,1);
});
jQuery('#srch_keyword').on('keypress', function(e){
if(e.keyCode == '13'){
e.preventDefault();
load_news_list(0,1);
}
});
jQuery('.pagination').on( 'click', '.page-numbers', function( event ) {
event.preventDefault();
page = find_page_number(jQuery(this));
var post_length = jQuery( '.news-div' ).length;
load_news_list( post_length, page);
});

 

/* load news list */
function load_news_list(post_length, page) {

var news_cats = jQuery('.categoryCheck:checked').map(function() {return this.value;}).get().join(',');
var year = ( jQuery( '#year' ).length > 0 ) ? jQuery( '#year' ).val() : '';
var monthnum = ( jQuery( '#monthnum' ).length > 0 ) ? jQuery( '#monthnum' ).val() : '';
var search_val = jQuery('#srch_keyword').val();
// jQuery( '.clear-all' ).css( 'display' , 'none' );
if( news_cats.length > 0 ) {
jQuery( '.clear-all' ).css( 'display' , 'inline-block' );
}

jQuery.ajax({
type: "POST",
url: adminurl,
data: { action : 'load_news', news_cats: news_cats, post_length : post_length, year: year, monthnum: monthnum ,search:search_val, page: page},
success: function (result){
var responseData = JSON.parse(result);
jQuery( '.load-more-news' ).remove();
jQuery( '.news-listing' ).empty();
jQuery( '.news-listing' ).append(responseData.news);
jQuery( '.pagination' ).empty()
jQuery( '.pagination' ).append(responseData.pagination);
},
error:function(){
alert("Error: There is some issue please try again.");
},
beforeSend:function(){
jQuery( '.load-result' ).css( 'display' , 'block' );
},
complete: function () {
jQuery( '.load-result' ).css( 'display' , 'none' );
}
});
}

 

WordPress code


add_action( 'wp_ajax_nopriv_load_news', 'load_news' );
add_action( 'wp_ajax_load_news', 'load_news' );
function load_news() {
$per_page = 12;
$paged = ( isset($_POST['page']) ) ? $_POST['page'] : 1 ;
$offset = (( $paged - 1 ) * $per_page);
/* if( !empty( $_POST['post_length'] ) ) { */
$post_length = $_POST['post_length'];
//$offset = $post_length;
$nextOffset = $offset + $per_page;
/* } */
$result = '';

$pagination = '';
$newsList = getNewsArray( $offset , $per_page , $_POST );

$result = '<div class="col-sm-12"><header class="page-header">';
$result .= '<h2 class="page-title">Sorry, it seems that we didn&rsquo;t find any results</h2>';
$result .= '</header>';
$result .= '<div class="page-content">';
$result .= '<p>Please try the search function at the top of the page to see if you can find what you&rsquo;re looking for</p>';
$result .= '</div></div>';
if ( $newsList->have_posts() ) {
$result = '';
while ( $newsList->have_posts() ) { $newsList->the_post();
$result .= post_html( $post->ID );
}
}

if($newsList->max_num_pages > 0){
$big = 999999999;
$pagination = paginate_links( array(
'type' => 'plain',
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, $paged ),
'total' => $newsList->max_num_pages,
'prev_next' => true,
'prev_text' => 'Prev',
'next_text' => 'Next',
) );
//$pagination .= football_pagination($newsList->max_num_pages);

} else {
$result .= '<div class="no-more-news"></div>';
}

$data['news'] = $result;
$data['pagination'] = $pagination;
echo json_encode( $data );

die();
}

&nbsp;
function getNewsArray( $offset , $per_page , $newsFilter ) {

$argsNews = array('post_type' => 'post', 'post_status'=> 'publish' , 'posts_per_page' => $per_page , 'offset' => $offset, 'order' => 'desc', 'orderby' => 'date' );

if( !empty($newsFilter['news_cats']) ) {
$cats = explode( ',' , $newsFilter['news_cats'] );
$argsNews['category__in'] = $cats;
}
if(!empty($newsFilter['year'])){
$argsNews['year'] = $newsFilter['year'];
}
if(!empty($newsFilter['monthnum'])){
$argsNews['monthnum'] = $newsFilter['monthnum'];
}
if(!empty($newsFilter['search'])){
$argsNews['s'] = $newsFilter['search'];
}
$newsList = new WP_Query( $argsNews );

return $newsList;
}

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s