Skip to content

Instantly share code, notes, and snippets.

@uzielweb
Created December 13, 2021 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uzielweb/322d0573d1dff0cfa7ac86f94c8a3a4d to your computer and use it in GitHub Desktop.
Save uzielweb/322d0573d1dff0cfa7ac86f94c8a3a4d to your computer and use it in GitHub Desktop.
Wordpress --- Filtering Events based on custom field inside custom post type
<?php
// developed by Daniel Paz and Uziel
$tipo = $_GET['tipo'];
$categoria = $_GET['categoria'];
?>
<form action="" method="get">
<div class="vistoboot">
<div class="container">
<div class="row align-items-end">
<div class="col-md">
<!-- Próximos Eventos, Eventos Fechados -->
<div class="form-group">
<!-- radio on change value send form -->
<input type="radio" name="tipo" value="futuros" onchange="this.form.submit()"<?php echo $tipo == 'futuros' || '' ? ' checked' : '';?>> <label>Próximos Eventos</label>
<input type="radio" name="tipo" value="passados" onchange="this.form.submit()"<?php echo $tipo == 'passados' ? ' checked' : '';?>> <label>Eventos Fechados</label>
</div>
</div>
<div class="col-md-auto">
<div class="form-group d-flex flex-column">
<label>Escolha a categoria de seu curso</label>
<select name="categoria" class="form-control" onchange="this.form.submit()">
<option value="">Todos</option>
<?php
$query = new WP_Query(array(
'post_type' => 'curso',
'post_status' => 'publish'
));
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
$post_title = get_the_title();
// slug
$post_slug = get_post_field('post_name', $post_id);
$selected = $categoria == $post_slug ? ' selected' : '';
echo '<option value="' . $post_slug . '"' . $selected . '>'.$post_title .'</option>';
}
?>
</select>
</div>
</div>
</div>
</div>
</div>
</form>
?>
<?php
// Wordpress retreive the content from a custom post type and display it if the start_date is before today
$tipo = $_GET['tipo'];
$categoria = $_GET['categoria'];
$targetDate = 'end_date';
$signalDate = '>=';
if ($categoria){
$curso = get_page_by_path($categoria,OBJECT,'curso');
$cursoId = $curso->ID;
}
if ($tipo == 'futuros'){
$targetDate = 'end_date';
$signalDate = '>=';
}
if ($tipo == 'passados'){
$targetDate = 'end_date';
$signalDate = '<';
}
$args = array(
'post_type' => 'evento',
'meta_key' => 'end_date',
// 'orderby' => 'meta_value',
// 'order' => 'ASC',
// 'posts_per_page' => 150,
'meta_query' => array(
array(
'key' => $targetDate,
'value' => date('Y-m-d H:i:s'),
'compare' => $signalDate,
'type' => 'DATE'
)
)
);
if ($categoria){
$args = array(
'post_type' => 'evento',
'meta_key' => 'end_date',
// 'orderby' => 'meta_value',
// 'order' => 'ASC',
// 'posts_per_page' => 150,
'meta_query' => array(
array(
'key' => $targetDate,
'value' => date('Y-m-d H:i:s'),
'compare' => $signalDate,
'type' => 'DATE'
),
array(
'key' => 'curso',
'value' => $cursoId,
'compare' => '=',
'type' => 'NUMERIC'
)
)
);
}
// show the posts
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) :?>
<div class="vistoboot eventos-modulo">
<div class="container">
<div class="row">
<?php while ( $the_query->have_posts() ) :?>
<?php $the_query->the_post(); ?>
<?php endwhile; ?>
<div class="evento-direita col-lg">
<?php while ( $the_query->have_posts() ) :?>
<?php $the_query->the_post(); ?>
<?php
// start_date and format to day
$day[$the_query->current_post] = date('d', strtotime(get_field('start_date')));
// short month wordpres in the current language
$montShort[$the_query->current_post] = date_i18n('M', strtotime(get_field('start_date')));
$year[$the_query->current_post] = date('Y', strtotime(get_field('start_date')));
$hour[$the_query->current_post] = date('H:i', strtotime(get_field('start_date')));
?>
<div class="evento">
<div class="row">
<div class="col-sm-auto">
<div class="data">
<div class="dia">
<?php echo $day[$the_query->current_post]; ?>
</div>
<div class="mes">
<?php echo $montShort[$the_query->current_post]; ?> - <?php echo $year[$the_query->current_post]; ?>
</div>
</div>
</div>
<div class="col-sm">
<h3 class="title"><?php the_title(); ?></h3>
<?php if ( get_field('periodo_evento') or get_field('local_evento') ) : ?>
<div class="detalhes">
<?php if ( get_field('periodo_evento') ) : ?>
<div class="periodo-evento">
<div class="icone">
<svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M10.0865 19.1709C4.98403 19.1709 0.847656 15.0346 0.847656 9.93208C0.847656 4.82961 4.98403 0.693237 10.0865 0.693237C15.189 0.693237 19.3253 4.82961 19.3253 9.93208C19.3197 15.0322 15.1867 19.1653 10.0865 19.1709ZM10.0865 2.54101C6.00452 2.54101 2.69543 5.8501 2.69543 9.93208C2.69543 14.0141 6.00452 17.3232 10.0865 17.3232C14.1685 17.3232 17.4776 14.0141 17.4776 9.93208C17.473 5.852 14.1666 2.54559 10.0865 2.54101ZM14.7059 10.856H9.16262V5.31266H11.0104V9.0082H14.7059V10.856Z" fill="#262626" />
</svg>
</div>
<?php echo get_field('periodo_evento'); ?>
</div>
<?php endif;?>
<?php if ( get_field('local_evento') ) : ?>
<div class="local-evento">
<div class="icone">
<svg width="13" height="17" viewBox="0 0 13 17" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M6.46719 16.6334C5.30027 15.638 4.21863 14.5468 3.2336 13.3711C1.75538 11.6056 8.16402e-07 8.9762 8.16402e-07 6.47062C-0.0013094 3.85376 1.57453 1.49402 3.99217 0.492532C6.40981 -0.508952 9.19271 0.0452267 11.0423 1.89647C12.2585 3.10732 12.9398 4.7544 12.9344 6.47062C12.9344 8.9762 11.179 11.6056 9.70079 13.3711C8.71576 14.5468 7.63412 15.638 6.46719 16.6334ZM6.46719 3.69897C5.47698 3.69897 4.56198 4.22724 4.06687 5.08479C3.57176 5.94235 3.57176 6.9989 4.06687 7.85645C4.56198 8.714 5.47698 9.24228 6.46719 9.24228C7.99794 9.24228 9.23885 8.00136 9.23885 6.47062C9.23885 4.93988 7.99794 3.69897 6.46719 3.69897Z" fill="#262626" />
</svg>
</div>
<?php echo get_field('local_evento'); ?>
</div>
<?php endif;?>
</div>
<?php endif;?>
</div>
<div class="col-lg-auto btn-mais-wrapper">
<div class="btn-mais">
<a class="btn btn-orange" href="<?php the_permalink(); ?>">INSCRIÇÃO</a>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
</div>
</div>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment