1

I am creating custom post type eventlist filter form. There is two options: category and months. Following this advice List events by month I got display event categories by month. Meaning...

January

  • Event post 1
  • Event post 2

February

  • Event post 1
  • Event post 2

etc.

Category choosing works smoothly. Problem comes up when user choose month. By default events are listed by month. How can I reorder events using form options? Is it possible solve using meta_query compare or do I need some other solution?

<form method="get" action="<?php the_permalink();?>" name="tapahtumalista">
<select class="form-select" name="kat">

<?php 
$terms = get_terms( array(
    'taxonomy'      => 'kategori_tapahtumat',
));


// SELECT OPTION CATEGORY
?>

    <option value="">Valitse kategoria</option>
    <?php 
    
    foreach ($terms as $term) { ?>
        <option value="<?php echo $term->slug; ?>" <?php if(isset($_GET['kat']) && $_GET['kat'] == $term->slug) { echo 'selected="selected"'; }?>>
        <?php echo $term->name; ?></option>
    <?php } ?>
</select>

<?php  // SELECT OPTION MONTH ?>
<select class="form-select" name="kk">
    <option value="">Valitse kuukausi</option>
    <?php 
    for ($i = 1; $i <= 12; $i++){
    $month_name = date_i18n('F', mktime(0, 0, 0, $i, 1, 2011)); 
    $month = !empty( $_GET['kk'] ) ? $_GET['kk'] : 0;
    $selected = $month_name == $month;
    ?>
    <option value="<?php echo $month_name; ?>" 
    <?php if(isset($_GET['kk']) && !empty($_GET['kk']) == $selected) { echo 'selected="selected"'; }?>
    <?php $selected ?>> 
        <?php echo $month_name; ?>
    </option>
    
    <?php }?>
</select>
<input type="submit" name="" value="Valitse">
// Selected category from form option 
if($_GET['kat'] && !empty($_GET['kat'])) {
    $selected_cat = $_GET['kat'];
}

//Selected month from form option
if($_GET['kk'] && !empty($_GET['kk'])) {
    $selected_month = $_GET['kk'];  
}


$today = date('Ymd');
$m = get_the_time('m');

$args =  [
    'post_type'         => 'tapahtumat',
    'post_status'       => 'publish',
    'posts_per_page'    => - 1,
    'meta_key'          => 'aloituspaiva', //event start date
    'meta_value'        => $today,
    'orderby'           => 'meta_value',
    'value'             => $today,
    'meta_compare'      => '>=',
    'type'              => 'DATE',
    'order'             => 'ASC',
    'tax_query'         => [
        [
            'taxonomy'  => 'kategori_tapahtumat',
            'field'     => 'slug',
            'terms'     => $selected_cat, //category get from form 
        ]
    ],
    'meta_query'        => [
        'relation'      => 'OR',
        [
            
            'month'     => $m,
            'value'     => $selected_month, // get from form possible?
            'compare'   => '=',
        ]

    ]
];
$query = new WP_Query($args);

// List events by month (default view) - using dboris solution

$all_events = [];

if($query->have_posts()) :
    while ($query->have_posts() ) : $query->the_post();
        $date = strtotime(get_post_meta( get_the_ID(), 'aloituspaiva', true ) );
        $month_year = date( "F", $date );
        $all_events[ $month_year ][] = $query->post;    
    endwhile;
    // Sorting events by month

    foreach ( $all_events as $month_year => $events) : ?>
        <?php echo '<br><br><strong>' . $month_year . '</strong><br>';  ?>

        <?php foreach ( $events as $event ) : ?>
            <?php echo $event->post_title . '<br>';?>
        <?php endforeach;?>
    <?php endforeach;
else :
    echo ('No results.');
endif;

wp_reset_postdata(); 
0

Problem solved!

Before - Select Month

<option value="01"></option>
<option value="02"></option>
etc.

Change - Select Month

<option value="20210101"></option>
<option value="20210201"></option>
etc.

//ARGS
$selected_cat = $_GET['cat']; //category value
$start_date = $_GET['month']; //month value
$end_date = $start_date + 30;

'meta_query' => [
[
'key' => 'event_date', //acf date picker field Ymd
'value' => array($start_date, $end_date),
'compare' => 'BETWEEN',
'type' => 'DATE',
]
]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.