Advertisement
Guest User

Untitled

a guest
Oct 1st, 2015
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.22 KB | None | 0 0
  1. function update_results(){
  2.     // Nonce check.
  3.     $nonce = $_REQUEST['nonce'];
  4.     if ( ! wp_verify_nonce( $nonce, 'ajax_search_filters_nonce' ) ) {
  5.         die( __('Busted.') ); // Nonce check
  6.     }
  7.  
  8.     // set is_search to true to that the results template can check it
  9.     global $wp_query;
  10.     $wp_query->is_search = true;
  11.  
  12.     $args = array();
  13.     $args['s'] = $_REQUEST['search'];
  14.     $args['post_type'] = array( 'news_article', 'perf_task', 'lesson_plan', 'diff_perf_task', 'diff_lesson_plan' );
  15.     $args['post_status'] = 'publish';
  16.     $args['tax_query'] = array( 'relation' => 'AND' );
  17.  
  18.     add_filter('posts_join', 'hsta_search_posts_join', 10, 2 );
  19.     add_filter('posts_where', 'hsta_search_posts_where', 10, 2 );
  20.  
  21.     $meta_args = array(
  22.         'relation' => 'OR',
  23.         array(
  24.             array(
  25.                 'key' => 'pt_assessment',
  26.                 'value' => 'rebel',
  27.                 'compare' => 'LIKE'
  28.             ),
  29.         ),
  30.         array( 'relation' => 'AND' ),
  31.     );
  32.  
  33.     // collect filter data
  34.     // apply post types
  35.     if ( array_key_exists( 'types', $_REQUEST ) && is_array( $_REQUEST['types'] ) && count( $_REQUEST['types'] ) > 0 ) {
  36.         $args['post_type'] = $_REQUEST['types'];
  37.     }
  38.  
  39.     // apply grade filter (if any)
  40.     if ( array_key_exists('grade', $_POST) && strlen( $_POST['grade'] ) > 0 ) {
  41.         $meta_args[1][] = array(
  42.             'key' => 'pt_grade_level',
  43.             'value' => $_POST['grade'],
  44.             'compare' => '=',
  45.         );
  46.     }
  47.  
  48.     // apply content area filter (if any)
  49.     if ( array_key_exists('contentArea', $_POST) && strlen( $_POST['contentArea'] ) > 0 ) {
  50.         $meta_args[1][] = array(
  51.             'key' => 'pt_content_area',
  52.             'value' => serialize( strval( $_POST['contentArea'] ) ),
  53.             'compare' => 'LIKE',
  54.         );
  55.     }
  56.  
  57.     // apply standard filter (if any)
  58.     if ( array_key_exists('standard', $_POST) && strlen( $_POST['standard'] ) > 0 ) {
  59.         $meta_args[1][] = array(
  60.             'key' => 'pt_standard',
  61.             'value' => $_POST['standard'],
  62.             'compare' => '=',
  63.         );
  64.     }
  65.  
  66.     // apply PD Course taxonomy filter (if any)
  67.     if ( array_key_exists('pdCourse', $_POST) && strlen( $_POST['pdCourse'] ) > 0 ) {
  68.         $args['tax_query'][] = array(
  69.             'taxonomy' => 'pd_courses',
  70.             'field' => 'id',
  71.             'terms' => $_POST['pdCourse'],
  72.             'operator' => 'IN',
  73.         );
  74.     }
  75.  
  76.     $meta_loop = new WP_Meta_Query( $meta_args );
  77.     global $wpdb;
  78.     $GLOBALS['meta_sql'] = $meta_loop->get_sql('post', $wpdb->posts, 'ID', $context = null );
  79.     $loop = new WP_Query( $args );
  80.     ob_start(); ?>
  81.     <hr>
  82.     <h1>Ajax Results</h1>
  83.     <?php
  84.     if ( ! $loop->have_posts() ) {
  85.  
  86.     }
  87.  
  88.     // render results
  89.     while ( $loop->have_posts() ) : $loop->the_post(); ?>
  90.         <?php
  91.             global $post;
  92.             setup_postdata( $post );
  93.         ?>
  94.         <?php get_template_part( 'templates/results', 'search' ); ?>
  95.     <!-- hsta_the_task_perf_card( $post ); -->
  96.     <?php endwhile;
  97.     wp_reset_postdata();
  98.  
  99.     $return = ob_get_contents();
  100.     ob_end_clean();
  101.  
  102.     wp_send_json_success( $return );
  103. }
  104. add_action( 'wp_ajax_update_results', 'update_results' );
  105. add_action( 'wp_ajax_nopriv_update_results', 'update_results' );
  106.  
  107. // filter
  108. function hsta_search_posts_join( $join ) {
  109.     $join .= $GLOBALS['meta_sql']['join'];
  110.     return $join;
  111. }
  112. function hsta_search_posts_where( $where, $wp_query ) {
  113.     $re = "/^ AND/";  
  114.     $subst = " OR";
  115.     $meta_where = preg_replace($re, $subst, $GLOBALS['meta_sql']['where']);
  116.     $where .= $meta_where;
  117.     return $where;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement