Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function update_results(){
- // Nonce check.
- $nonce = $_REQUEST['nonce'];
- if ( ! wp_verify_nonce( $nonce, 'ajax_search_filters_nonce' ) ) {
- die( __('Busted.') ); // Nonce check
- }
- // set is_search to true to that the results template can check it
- global $wp_query;
- $wp_query->is_search = true;
- $args = array();
- $args['s'] = $_REQUEST['search'];
- $args['post_type'] = array( 'news_article', 'perf_task', 'lesson_plan', 'diff_perf_task', 'diff_lesson_plan' );
- $args['post_status'] = 'publish';
- $args['tax_query'] = array( 'relation' => 'AND' );
- add_filter('posts_join', 'hsta_search_posts_join', 10, 2 );
- add_filter('posts_where', 'hsta_search_posts_where', 10, 2 );
- $meta_args = array(
- 'relation' => 'OR',
- array(
- array(
- 'key' => 'pt_assessment',
- 'value' => 'rebel',
- 'compare' => 'LIKE'
- ),
- ),
- array( 'relation' => 'AND' ),
- );
- // collect filter data
- // apply post types
- if ( array_key_exists( 'types', $_REQUEST ) && is_array( $_REQUEST['types'] ) && count( $_REQUEST['types'] ) > 0 ) {
- $args['post_type'] = $_REQUEST['types'];
- }
- // apply grade filter (if any)
- if ( array_key_exists('grade', $_POST) && strlen( $_POST['grade'] ) > 0 ) {
- $meta_args[1][] = array(
- 'key' => 'pt_grade_level',
- 'value' => $_POST['grade'],
- 'compare' => '=',
- );
- }
- // apply content area filter (if any)
- if ( array_key_exists('contentArea', $_POST) && strlen( $_POST['contentArea'] ) > 0 ) {
- $meta_args[1][] = array(
- 'key' => 'pt_content_area',
- 'value' => serialize( strval( $_POST['contentArea'] ) ),
- 'compare' => 'LIKE',
- );
- }
- // apply standard filter (if any)
- if ( array_key_exists('standard', $_POST) && strlen( $_POST['standard'] ) > 0 ) {
- $meta_args[1][] = array(
- 'key' => 'pt_standard',
- 'value' => $_POST['standard'],
- 'compare' => '=',
- );
- }
- // apply PD Course taxonomy filter (if any)
- if ( array_key_exists('pdCourse', $_POST) && strlen( $_POST['pdCourse'] ) > 0 ) {
- $args['tax_query'][] = array(
- 'taxonomy' => 'pd_courses',
- 'field' => 'id',
- 'terms' => $_POST['pdCourse'],
- 'operator' => 'IN',
- );
- }
- $meta_loop = new WP_Meta_Query( $meta_args );
- global $wpdb;
- $GLOBALS['meta_sql'] = $meta_loop->get_sql('post', $wpdb->posts, 'ID', $context = null );
- $loop = new WP_Query( $args );
- ob_start(); ?>
- <hr>
- <h1>Ajax Results</h1>
- <?php
- if ( ! $loop->have_posts() ) {
- }
- // render results
- while ( $loop->have_posts() ) : $loop->the_post(); ?>
- <?php
- global $post;
- setup_postdata( $post );
- ?>
- <?php get_template_part( 'templates/results', 'search' ); ?>
- <!-- hsta_the_task_perf_card( $post ); -->
- <?php endwhile;
- wp_reset_postdata();
- $return = ob_get_contents();
- ob_end_clean();
- wp_send_json_success( $return );
- }
- add_action( 'wp_ajax_update_results', 'update_results' );
- add_action( 'wp_ajax_nopriv_update_results', 'update_results' );
- // filter
- function hsta_search_posts_join( $join ) {
- $join .= $GLOBALS['meta_sql']['join'];
- return $join;
- }
- function hsta_search_posts_where( $where, $wp_query ) {
- $re = "/^ AND/";
- $subst = " OR";
- $meta_where = preg_replace($re, $subst, $GLOBALS['meta_sql']['where']);
- $where .= $meta_where;
- return $where;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement