Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Filtro de Noticias
- add_action( 'wp_ajax_myfilter', 'ajax_filter_news' );
- add_action( 'wp_ajax_nopriv_myfilter', 'ajax_filter_news' );
- function ajax_filter_news() {
- // Inicializando
- $date_filter = [];
- $tax_query = [ 'relation' => 'AND' ];
- // Filtro de Categoria
- if ( ! empty( $_POST['areafilter'] ) ) {
- $tax_query[] = array(
- 'taxonomy' => 'category',
- 'field' => 'id',
- 'terms' => sanitize_text_field( $_POST['areafilter'] ),
- );
- }
- // Filtro de Tag
- if ( ! empty( $_POST['assuntofilter'] ) ) {
- $tax_query[] = array(
- 'taxonomy' => 'post_tag',
- 'field' => 'id',
- 'terms' => sanitize_text_field( $_POST['assuntofilter'] ),
- );
- }
- // Filtro do Mês
- $month = ( empty( $_POST['mesfilter'] ) ) ? 0 : sanitize_text_field( $_POST['mesfilter'] );
- if ( $month => 1 && $month <= 12 ) {
- $date_filter['month'] = $month;
- }
- // Filtro do Ano
- $year = ( empty( $_POST['anofilter'] ) ) ? 0 : sanitize_text_field( $_POST['anofilter'] );
- if ( $year => 1000 && $year <= 9999 ) {
- $date_filter['year'] = $year;
- }
- // Order
- $order = 'DESC';
- if ( ! empty( $_POST['date'] ) && strtoupper( sanitize_text_field( $_POST['date'] ) ) === 'ASC' ) {
- $order = 'ASC';
- }
- // Argumentos
- $args = array(
- 'post_type' => 'post',
- 'posts_per_page' => -1,
- 'orderby' => 'date',
- 'order' => $order,
- );
- if ( count( $tax_query ) > 1 ) {
- $args['tax_query'] = $tax_query;
- }
- if ( ! empty( $date_filter ) ) {
- $args['date_query'] = [ $date_filter ];
- }
- // A Query
- $query = new WP_Query( $args );
- if ( $query->have_posts() ) :
- while( $query->have_posts() ): $query->the_post();
- echo '<article id="post-'. $query->post->ID.'" class="post-default ajax"> ';
- echo '<a href="'.get_the_permalink().'">';
- echo '<img src="'.get_the_post_thumbnail_url().'" />';
- echo '</a>';
- echo '<div class="post-default__meta post-meta">';
- echo'<div class="post-meta__column">';
- echo'<span class="post-meta__date">'.get_the_date().'</span>';
- echo'</div>';
- echo'</div>';
- echo '<h2 class="h1"><a href="'.get_the_permalink().'">'.get_the_title().'</a></h2>';
- echo '<p>'.the_excerpt().'</p>';
- echo '<div class="post-default__footer post-footer">
- <div class="post-footer__column" style="width: 42% !important;"> Área:';
- echo '<p><span>'.the_category('Assunto:', '<br />').'</span>
- <span> '.the_tags( ' / Assunto: ', '<br />').'</span></p>';
- echo '<a href="'.get_the_permalink().'">saiba mais</a>
- </div>';
- echo '<div class="post-footer__column">
- <span class="post-footer__comments">'.comments_number( '(0) Comentários', '(1) Comentário', '(%) Comentários' ).'</span>
- </div>';
- echo '</article>';
- endwhile;
- wp_reset_postdata();
- else :
- echo '<h1> Nenhuma notícia encontrada </h1>';
- endif;
- die();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement