Comparing Checkbox Field to Current Date within Loop - php

I have my checkbox custom field (show_days) as values (1-7) to represent (Moo-Sun) and I tested it with an echo and it is returning the correct value. Example today is Tuesday and the echo is showing 2.
I am trying to compare it to the current date strftime("%u", time()); to only show posts that have the current day check marked. Its not working and Im wondering if I need to add an in_array somewhere. Thanks for your help.
<?php
$days = get_field('show_days');
$date = strftime("%u", time());
if ($query->have_posts() && $days = $date ) { while( $query->have_posts() ) {
$query->the_post();
echo '<div class="onAir"><h3>Currently On Air: ';
the_title();
echo $days. '</h3></div>';
} wp_reset_postdata();
}
?>

You need to add the comparison ($days = $date) as a meta_query to the WP_Query arguments.
<?php
$datetime = date('Y-m-d');
$day_number = date('w', strtotime($datetime));
$args = array(
'post_type' => 'your_post_type',
'meta_key' => 'show_days',
'meta_value' => $day_number,
'meta_compare' => '=',
);
$query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="onAir">
<h3>Currently On Air: <?php the_title(); ?> <?php echo $day_number; ?></h3>
</div>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php endif; ?>

Alrighty...after a lot of different comparisons and pulling out of my hair, I got it sorted it out. Comparing two custom field time ranges with the current time, and then comparing the selected day of the week with the current day and WP Query the posts and on top of that....my Wordpress timestamp was 6 hours ahead. Ugh! haha, This code did it
<?php
$time = current_time('H:i:s');
date_default_timezone_set('America/Denver');
$date = strftime("%A", time());
$shows = get_field('station_shows', false, false);
$query = new WP_Query ( array(
'post_type' => 'shows',
'posts_per_page' => 1,
'post__in' => $shows,
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_time',
'value' => $time,
'compare' => '<=',
'type' => 'TIME',
),
array(
'value' => $time,
'key' => 'end_time',
'compare' => '>=',
'type' => 'TIME',
),
'relation' => 'AND',
array(
'key' => 'show_days',
'value' => $date,
'compare' => 'LIKE',
)
)));
if ($query->have_posts() ) { while( $query->have_posts() ) {
$query->the_post();
echo '<div class="onAir"><h3>Currently <span>On Air</span> : ';
the_title();
echo '</h3></div>';
}
wp_reset_postdata();
}
?>

Related

Manipulate a default query doesn't work as expected in wordpress

I'm trying to manipulate a default query on my archive-event.php page. What I want to query is to order events(my custom posts) by the closest event (based on it's date).
This is my code in functions.php:
function university_adjust_queries ($query) {
if(!is_admin() && is_post_type_archive('event') && $query->is_main_query()) {
$today = date('Ymd');
$query->set('meta_key','event_date');
$query->set('orderby','meta_value_num');
$query->set('order','ASC');
$query->set('meta_query', array(
array(
'key' => 'event_date',
'compare' => '>=',
'value' => $today,
'type' => 'numeric'
),
),);
}
}
add_action('pre_get_posts','university_adjust_queries');
and this is my code in archive-event.php :
while ( have_posts() ) {
the_post();
?>
<div class="event-summary">
<a class="event-summary__date t-center" href="#">
<span class="event-summary__month">
<?php
try {
$event_Date = get_field( 'event_date' );
$event_Date = date( 'Y-m-d', strtotime( $event_Date));
$eventDate = new DateTime($event_Date);
} catch ( Exception $e ) {
echo 'error happened';
}
echo $eventDate->format( 'M' );
?></span>
<span class="event-summary__day"><?php
echo $eventDate->format('d');
?></span>
</a>
...
But the problem is even with different dates that my events have in my admin dashboard , their dates are shown entirely wrong (some are duplicates!)
This is the image of my archive-event page :
Have you tried changing the type in your meta query to DATE?
$query->set('meta_query', array(
array(
'key' => 'event_date',
'compare' => '>=',
'value' => $today,
'type' => 'DATE'
),
),);

ACF Repeater for Event Dates. Repeat post in loop based on multiple dates in post repeater fields

I currently have a query for an "Event" category that looks for the ACF event_date field.
Can I use the ACF Repeater option to add multiple event dates to a single post, and then update the query so that it will repeat the same post with multiple event dates in the loop multiple times in order based on the event dates associated with post?
Will that work, repeat a post in the loop using the ACF repeater for multiple event dates?
Current Query with single event_date field.
<?php
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'events'
)
),
'meta_query' => array(
array(
'key' => 'event_date',
'value' => date("Y-m-d"),
'compare' => '>',
),
),
'posts_per_page' => 5,
'meta_key' => 'event_date',
'orderby' => 'meta_value',
'meta_type' => 'NUMERIC',
'order' => 'ASC'
);
$query = new WP_Query( $args );
?>
<?php if ( $query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php
$date = null;
if( get_field("event_date") ){
$date = date_format( date_create( get_field("event_date") ), "F j, Y" );
}
?>
<div class="program-event-wrapper">
<?php if( $date): ?>
<h5><?php echo $date; ?></h5>
<?php endif; ?>
<p>
<?php the_title(); ?>
</p>
</div>
<?php endwhile; ?>
<!-- end of the loop -->
<?php //wp_reset_postdata(); ?>
<?php endif; ?>
I think I have gauged what you are after.
<?php if( have_rows('your_repeater_field_name') ): ?>
<p><?php the_title(); ?></p>
<?php while ( have_rows('your_repeater_field_name') ) : the_row(); ?>
<?php
$date = null;
if( get_sub_field("event_date") ){
$date = date_format( date_create( get_sub_field("event_date") ), "F j, Y" );
}
?>
<div class="program-event-wrapper">
<?php if( $date): ?>
<h5><?php echo $date; ?></h5>
<?php endif; ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
I have created the ACF Repeater for your dates where if at least one date exists, the title and link are shown. Then, the ACF repeater will go through each row and get the sub field (named event_date) and attached it to your variable and you would run your code you have provided to echo the date, all within the ACF while loop.
FYI, you would replace your_repeater_field_name with your repeater field name.
I hope this makes sense but let me know if you need anything expanding (if I have gauged what you are after correctly).

Custom loop not displaying all available posts

I am having trouble with a custom Wordpress / ACF loop I am working on.
The idea is that it displays the latest posts within the 'events' post type, hiding any posts where the event date has passed.
The posts do hide if the date has passed. However the loop is not displaying the full amount of posts available. Currently with the loop below, it is only showing 6 out of the available 10.
I have checked the reading settings in Wordpress and that's fine.
The code I am using for my loop is:
<ul class="events-list">
<?php
$loop = new WP_Query( array(
'post_type' => 'events',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_type' => 'DATE',
'meta_key' => 'event-date'
));
while ( $loop->have_posts() ) : $loop->the_post();
$today = date('dmY');
$expire = get_field('event-date');
if( $expire > $today )
{ ?>
<li>
<h3><?php the_field('event-date'); ?> - <?php the_title(); ?></h3>
<span class="time"><?php the_field('event-time'); ?></span>
<?php the_field('event-details'); ?>
</li>
<?php; } endwhile; wp_reset_query(); ?>
</ul>
If you're going to compare dates then you need to convert them to the appropriate types. Convert them to Unix timestamp then you can easily compare when the date has surpassed. At the moment you are comparing which string is greater than the other which works sometimes but it's much more reliable to use Unix timestamp as your date formats always need to match.
if(strtotime(get_field('event-date')) > date('U')) {
//Your code here
}
Simply print compared dates before the "if" statement, and you will see where you made a mistake.
echo $expire.'__'.$today.'<br>';
if( $expire > $today )
It can be because of invalid date format, empty $expire field etc.. Anyway, you will see what the reason is after implementing that print.
The solution to this problem was to change the loop to:
<?php
$today = date('Ymd');
$loop = new WP_Query( array(
'post_type' => 'events',
'showposts' => 2,
'meta_key' => 'event-date',
'meta_compare' => '>',
'meta_value' => date("Ymd"),
'orderby' => 'meta_value_num',
'order' => 'ASC'
));
while ( $loop->have_posts() ) : $loop->the_post();
{ ?>
Post stuff here
<?php; } endwhile; wp_reset_query(); ?>

Wordpress custom loop displays posts based on the day...is posssible?

I would create a custom loop in the home page that will display only the posts that contains a specific word in its custom field.I'l try to explain it with an example.
EX. I got a website of a Basketball team and 2 custom post types called "COACh" and "TRAINING".
In "COACH" i add the coach and its skills.
In "TRAINING" i add the exrcise's name,the description and in the custom meta box i add the time,the coach,the duration and the day of the execution.
Now,suppose that it's Monday i would display all the TRAININGS that contains the day Monday in its custom field.
Is it possible without a plugin like event calendar????
IS IT RIGHT?
$today = date("l");
$args = array( 'post_type' => 'palinsesto', 'posts_per_page' => -1);
$palinsesto = get_posts( $args );
foreach ( $training as $post ) {
$day = get_post_meta( $post->ID, 'giorno ' ); // here day is key
if($day==$today)
while ($post->have_posts()) : $post->the_post(); ?>
Yes it is possible, I coded below roughly:
$args = array( 'post_type' => 'TRAINING', 'posts_per_page' => -1);
$training = get_posts( $args );
foreach ( $training as $post ) {
$day = get_post_meta( $post->ID, 'day ' ); // here day is key
if($day=='Monday')
echo $post->post_title.'<br />';
echo $post->post_content.'<br />';
}
this one is the solution for my question...it works great
$today = strftime('%A');
$day = get_post_meta( $post->ID, 'giorno ' );
$my_query = new WP_Query(array(
'post_type' => 'palinsesto',
'meta_query' => array(
array(
'key' => 'giorno',
'meta-value' => $value,
'value' => $today,
'compare' => '=',
))));
if (have_posts()) :
while ($my_query->have_posts()) : $my_query->the_post(); ?>

Filter out event posts older than current date

how do i filter out event posts who's date is older than the current date? my current code adds an expired class to the event if that event has expired, but becuase i need to display the next 5 upcoming posts the current code actually displays no event posts at all... here's my code....
<?php
wp_reset_query();
query_posts(array('post_type' => 'events',
'showposts' => 5,
'meta_key'=>'event_date',
'orderby' => 'meta_value',
'order' => ASC));
?>
<?php while (have_posts()) : the_post(); ?>
<?php
$eventDate = DateTime::createFromFormat('Ymd', get_field('event_date'));
$currentDate = new DateTime();
?>
<li class="<? if ($eventDate < $currentDate) { echo "expired"; } ?>">
<h4><?php the_title(); ?></h4>
<span class="date"><strong>Event Date:</strong> <? echo $eventDate->format('d/m/Y'); ?></span></li>
<?php endwhile;?>
pleas help someone :(
OK to answer your question. You can compare dates using a meta_value, but bear in mind that those dates need to be in the Mysql format YYYY-MM-DD for a comparison to be made in the database. So your custom field needs to be in that format or you need to reformat it in your code before you use it. Here's a good thread on the Wordpress Stack on that;
https://wordpress.stackexchange.com/questions/18303/fail-to-compare-dates-in-meta-query
I've not tested this but it should point you in the right direction;
$eventDate = date('Y-m-t', get_field('event_date'));
$currentDate = date('Y-m-t');
$args = array(
'post_type' => 'events',
'posts_per_page' => '-1',
'orderby' => 'meta_value',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => $eventDate,
'value' => $currentDate,
'compare' => '>',
'type' => 'DATE'
)
)
);
The other way to do it of course is just to pull all the posts into your query, set up a counter and display the first five which fulfil your criteria;
<?php
$counter = 0;
query_posts(array('post_type' => 'events',
'posts_per_page' => -1,
'meta_key'=>'event_date',
'orderby' => 'meta_value',
'order' => ASC));
while (have_posts()) : the_post();
$eventDate = DateTime::createFromFormat('Ymd', get_field('event_date'));
$currentDate = new DateTime();
if ( ($eventDate > $currentDate) && $counter<5) : ?>
<li class="<? if ($eventDate < $currentDate) { echo "expired"; } ?>">
<h4><?php the_title(); ?></h4>
<span class="date"><strong>Event Date:</strong> <? echo $eventDate->format('d/m/Y'); ?></span></li>
<?php
$counter++;
endif; ?>

Categories