Home › Forums › Front-end Issues › Filtering/Grouping Archive Page by checkbox value.
I’m trying to create a Calender display on a WordPress website. On each day a list of activities that is available ill be show.
At the moment I have it set up so each activity is a new custom post type (‘activity’) and on the post edit page, using Advanced Custom fields and the check box option (field name ‘day’) you can select monday Tuesday etc.
I can display the days its available on the activity post (i.e ‘Football’ or ‘Swimming’) through the following code:
<?php
/*
* Conditional statement (Checkbox value is an array)
*/
if( in_array( 'tuesday', get_field('day') ) )
{ ?>
<?php the_title();?>
<?php } ?>
But I want all posts to be displayed on a Summary/Archive page within the correct day – currently set up like so:
<div class="day-container">
<span class="day">Tu<span class="not-mobile">esday</span></span>
<ul>
<li class="swimming"> <div class="icon"> </div> Swimming</li>
<li class="fitness"> <div class="icon"> </div> Fitness</li>
<li class="walks"> <div class="icon"> </div> Health Walks</li>
</ul>
</div>
<div class="day-container">
<span class="day">Tu<span class="not-mobile">esday</span></span>
<ul>
<li class="swimming"> <div class="icon"> </div> Swimming</li>
<li class="fitness"> <div class="icon"> </div> Fitness</li>
<li class="walks"> <div class="icon"> </div> Health Walks</li>
</ul>
</div>
... etc
Fairly new to PHP so any help would be appreciated!
Sorry forgot to add code. I have this for my archive page:
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'activity',
'meta_query' => array(
'key' => 'day',
'compare' => '='
)
);
// get results
$the_query = new WP_Query( $args );
// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a>"><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
This works until I add the ‘value’ to the array, eg ‘value’ => ‘tuesday’, but nothing then appears.
my check boxes are set up like so:
monday : Monday
tuesday : Tuesday etc
After a while of plugging away I managed it, for anyones future reference:
<?php
$weekdays = array(
'monday' => 'Monday',
'tuesday' => 'Tuesday',
'wednesday' => 'Wednesday',
'thursday' => 'Thursday',
'friday' => 'Friday',
'saturday' => 'Saturday',
'sunday' => 'Sunday',
);
?>
<?php
foreach($weekdays as $day):
// args
$args = array(
'numberposts' => -1,
'post_type' => 'activity',
//'meta_key' => 'day',
//'meta_value' => $day
'meta_query' => array(
array(
'key' => 'day',
'value' => $day,
'compare' => 'LIKE'
)
)
);
// get results
$the_query = new WP_Query( $args );
// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
<div class="day-container<?php echo strtolower(date('l')) == strtolower($day) ? ' active' : ''; ?>">
<span class="day"><?php echo substr($day,0,2); ?><span class="not-mobile"><?php echo substr($day,2); ?></span></span>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li class="<?php echo strtolower(get_the_title()); ?>">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
<?php endif; ?>
<?php endforeach; ?>
The topic ‘Filtering/Grouping Archive Page by checkbox value.’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.