function dtwd_special_search_shortcode(){ ob_start(); ?> <div id="special-search-shortcode"> <!-- <form method="get" id="searchform" class="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>" role="search"> --> <fieldset> <!-- <input class="special-search-input-box" type="text" name="s" value="" placeholder="enter search keywords…" maxlength="50" required="required" /> --> <input type="hidden" name="cat_sname" value="alco-market-insights"> <input type="hidden" name="s"> <div class="col-4"> <label>Select Month</label> <select name="month_val" class="special-search-select-box" required id="month_val"> <!-- <option value="1">January</option> <option value="2">Febuary</option> <option value="3">March</option> --> <option value="4">April</option> <option value="5">May</option> <option value="6">June</option> <option value="7">July</option> <option value="8">August</option> <option value="9">September</option> <option value="10">October</option> <option value="11">November</option> <option value="12">December</option> </select> </div> <div class="col-8"> <label>Select Year</label> <select name="year_val" required id="year_val"> <option value=""><?php echo esc_attr( __( 'Select Year' ) ); ?></option> <option value="2020">2020</option> </select> <button type="submit" id="get_results">Search</button> </div> </fieldset> <!-- </form> --> </div> <div class="data_container"> <p class="loading">Loading Alco Market Insights</p> <div id="datafetch"></div> </div> <style> .loading{ display: none; } .loading:after { content: ' .'; animation: dots 1s steps(5, end) infinite; } @keyframes dots { 0%, 20% { color: rgba(0,0,0,0); text-shadow:.25em 0 0 rgba(0,0,0,0),.5em 0 0 rgba(0,0,0,0); } 40% { color: #000; text-shadow: .25em 0 0 rgba(0,0,0,0), .5em 0 0 rgba(0,0,0,0); } 60% { text-shadow: .25em 0 0 #000, .5em 0 0 rgba(0,0,0,0); } 80%, 100% { text-shadow: .25em 0 0 #000, .5em 0 0 #000; } } .data_container { margin-top: 25px; } p.meta_info { font-size: 14px; line-height: 16px; } .data_container .loop_items { margin-bottom: 20px; -webkit-box-shadow: 0 1px 4px rgba(56, 98, 64, 0.3), 0 0 40px rgba(56, 98, 64, 0.1) inset; -moz-box-shadow: 0 1px 4px rgba(56, 98, 64, 0.3), 0 0 40px rgba(56, 98, 64, 0.1) inset; box-shadow: 0 1px 4px rgba(56, 98, 64, 0.3), 0 0 40px rgba(56, 98, 64, 0.1) inset; padding: 15px; } .read_more { background: #386240; color: #fff; padding: 10px 15px; clear: both; font-size: 14px; display: block; max-width: 110px; text-align: center; line-height: 16px; margin-top: 10px; } .read_more:hover, a.read_more:focus { background: #222222; } </style> <?php $specialsearch = ob_get_clean(); return $specialsearch; } add_shortcode('special-search', 'dtwd_special_search_shortcode'); add_action( 'wp_footer', 'ajax_fetch_posts' ); function ajax_fetch_posts() { ?> <script> jQuery('#get_results').on('click', function() { jQuery('.loading').show(); jQuery('#datafetch').hide(); jQuery.ajax({ url: '<?php echo admin_url('admin-ajax.php'); ?>', type: 'post', data: { action: 'data_fetch_alco', month_s: jQuery('#month_val option:selected').val(), year_s: jQuery('#year_val option:selected').val(), }, success: function(data) { jQuery('#datafetch').html( data ); jQuery('.loading').hide(); jQuery('#datafetch').show(); console.log(data); }, error: function() { console.log("Error"); } }); }); </script> <?php } add_action('wp_ajax_data_fetch_alco' , 'data_fetch_alco'); add_action('wp_ajax_nopriv_data_fetch_alco','data_fetch_alco'); function data_fetch_alco(){ $the_query = new WP_Query( array( 'posts_per_page' => -1, //'s' => esc_attr( $_POST['keyword'] ), 'post_type' => 'post' , 'category_name'=> 'alco-market-insights', 'year'=>$_POST['year_s'], 'monthnum'=>$_POST['month_s'] ) ); if( $the_query->have_posts() ) : while( $the_query->have_posts() ): $the_query->the_post(); ?> <div class="loop_items"> <h2><a href="<?php echo esc_url( post_permalink() ); ?>"><?php the_title();?></a></h2> <p class="meta_info"><time datetime="<?php echo get_the_date('c'); ?>" itemprop="datePublished"><?php echo get_the_date(); ?></time> | <?php $categories = get_the_category(); if ( ! empty( $categories ) ) { echo '<a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a>'; }?></p> <a href="<?php echo esc_url( post_permalink() ); ?>" class="read_more">Read More</a> </div> <?php endwhile; else : echo '<h2>No results found</h2>'; wp_reset_postdata(); endif; die(); } function be_search_query( $query ) { if( $query->is_main_query() && ! is_admin() && $query->is_search() ) { if( !empty( $_GET['cat_sname'] ) ){ $query->set( 'category_name', esc_attr( $_GET['cat_sname'] ) ); $query->set( 'date_query', array( array( 'after' => $_GET['month_val']. '1,' .$_GET['year_val'], ) ) ); } } } add_action( 'pre_get_posts', 'be_search_query' );
Jagdish Sarma Asked question October 14, 2020