Advertisement
Guest User

Untitled

a guest
Nov 1st, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.43 KB | None | 0 0
  1. <?php
  2.  
  3. // Filtro de Noticias
  4. add_action( 'wp_ajax_myfilter', 'ajax_filter_news' );
  5. add_action( 'wp_ajax_nopriv_myfilter', 'ajax_filter_news' );
  6.  
  7. function ajax_filter_news() {  
  8.     // Inicializando
  9.     $date_filter = [];
  10.     $tax_query = [ 'relation' => 'AND' ];
  11.  
  12.     // Filtro de Categoria
  13.     if ( ! empty( $_POST['areafilter'] ) ) {
  14.         $tax_query[] = array(
  15.             'taxonomy'  => 'category',
  16.             'field'     => 'id',
  17.             'terms'     => sanitize_text_field( $_POST['areafilter'] ),
  18.         );
  19.     }
  20.  
  21.     // Filtro de Tag
  22.     if ( ! empty( $_POST['assuntofilter'] ) ) {
  23.         $tax_query[] = array(
  24.             'taxonomy'  => 'post_tag',
  25.             'field'     => 'id',
  26.             'terms'     => sanitize_text_field( $_POST['assuntofilter'] ),
  27.         );
  28.     }
  29.  
  30.     // Filtro do Mês
  31.     $month = ( empty( $_POST['mesfilter'] ) ) ? 0 : sanitize_text_field( $_POST['mesfilter'] );
  32.     if ( $month => 1 && $month <= 12 ) {
  33.         $date_filter['month'] = $month;
  34.     }
  35.  
  36.     // Filtro do Ano
  37.     $year = ( empty( $_POST['anofilter'] ) ) ? 0 : sanitize_text_field( $_POST['anofilter'] );
  38.     if ( $year => 1000 && $year <= 9999 ) {
  39.         $date_filter['year'] = $year;
  40.     }
  41.  
  42.     // Order
  43.     $order = 'DESC';
  44.     if ( ! empty( $_POST['date'] ) && strtoupper( sanitize_text_field( $_POST['date'] ) ) === 'ASC' ) {
  45.         $order = 'ASC';
  46.     }
  47.  
  48.     // Argumentos
  49.     $args = array(
  50.         'post_type'      => 'post',
  51.         'posts_per_page' => -1,
  52.         'orderby'        => 'date',
  53.         'order'          => $order,
  54.     );
  55.  
  56.     if ( count( $tax_query ) > 1 ) {
  57.         $args['tax_query'] = $tax_query;
  58.     }
  59.  
  60.     if ( ! empty( $date_filter ) ) {
  61.         $args['date_query'] = [ $date_filter ];
  62.     }
  63.  
  64.     // A Query
  65.     $query = new WP_Query( $args );
  66.  
  67.     if (  $query->have_posts() ) :
  68.         while( $query->have_posts() ):  $query->the_post();
  69.            
  70.            echo '<article id="post-'. $query->post->ID.'"  class="post-default ajax"> ';
  71.            echo '<a href="'.get_the_permalink().'">';      
  72.            echo '<img src="'.get_the_post_thumbnail_url().'" />';
  73.            echo '</a>';
  74.            echo '<div class="post-default__meta post-meta">';
  75.                     echo'<div class="post-meta__column">';
  76.                          echo'<span class="post-meta__date">'.get_the_date().'</span>';
  77.                     echo'</div>';                    
  78.             echo'</div>';
  79.             echo '<h2 class="h1"><a href="'.get_the_permalink().'">'.get_the_title().'</a></h2>';
  80.             echo '<p>'.the_excerpt().'</p>';
  81.             echo '<div class="post-default__footer post-footer">
  82.                    <div class="post-footer__column" style="width: 42% !important;"> Área:';
  83.             echo      '<p><span>'.the_category('Assunto:', '<br />').'</span>
  84.                       <span> '.the_tags( ' / Assunto: ', '<br />').'</span></p>';
  85.             echo       '<a href="'.get_the_permalink().'">saiba mais</a>
  86.                    </div>';
  87.             echo    '<div class="post-footer__column">
  88.                       <span class="post-footer__comments">'.comments_number( '(0) Comentários', '(1) Comentário', '(%) Comentários' ).'</span>
  89.                    </div>';  
  90.            echo '</article>';
  91.            
  92.         endwhile;
  93.        
  94.         wp_reset_postdata();
  95.     else :
  96.         echo '<h1> Nenhuma notícia encontrada </h1>';
  97.     endif;
  98.  
  99.     die();
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement