I am trying to detect date if it's greater or not to change post status, in result i am not getting proper way past dates are being greater or sometime future dates its too confusing why dates are not being compared
2020-02-05 (today date)
2020-01-20 (event date)
still it getting detect as greater date for event,
<?php
$loop = new WP_Query( array(
'post_type' => 'events',
'posts_per_page'=> -1,
)
);
?>
<?php while ( $loop->have_posts() ) : $loop->the_post();
$today = date("Y-m-d");
$date2 = get_field('event_date');
$event_date = $date2;
$expire_dt = $today;
echo '<br>';
echo $expire_dt;
echo '<br>';
echo $event_date;
echo '<br>';
if ($expire_dt < $event_date) {
?>
<?php
}else{
echo $today;
echo $event_date;
$my_post = array();
$my_post['ID'] = get_the_ID();
$my_post['post_status'] = 'draft';
// Update the post into the database
wp_update_post( $my_post );
?>
<?php
}
endwhile; wp_reset_query(); ?>
Wrap the 'event_date' as a DateTime -> new \DateTime(get_field('event_date'))
Try this:
$loop = new WP_Query([
'post_type' => 'events',
'posts_per_page' => -1,
]
);
while ($loop->have_posts()) {
$loop->the_post();
$today = new \DateTime();
$event_date = new \DateTime(get_field('event_date'));
// If `get_field()` doesn't work: try `the_field()`...
// $event_date = new \DateTime(the_field('event_date'));
if ($today >= $event_date) {
$my_post = [];
$my_post['ID'] = get_the_ID();
$my_post['post_status'] = 'draft';
wp_update_post($my_post);
}
}
wp_reset_query();
Related
I have a custom post called Project.
I need to automatically update a field with 100 when the post date is over 7 days.
I tried the following coed in function.php but it doesn’t auto update the field.
Would you please let me know how to fix the code?
function update_active_based_on_date() {
// query to get all custom posts - project
$args = array(
'post_type' => 'project',
'posts_per_page' => -1
);
$query = new WP_Query($args);
// if posts are returned and more than 7days, update infosubmit field
if ($query->have_posts()) {
global $post;
$timestamp = get_the_time( 'U', $post->ID );
$diff = current_time('timestamp') - $timestamp;
while ($query->have_posts() && $diff > 604800) {
$query->the_post();
$field_key = "field_60836f942ae12";
$value = "100";
update_field($field_key, $value, $post->ID);
} // end while have_posts
wp_reset_postdata();
} // end if have_posts
} // end function
Thank you.
You can achieve this like:
function update_active_based_on_date() {
// query to get all custom posts - project
$args = array(
'post_type' => 'project',
'posts_per_page' => -1
);
$query = new WP_Query($args);
if ($query->have_posts()) {
global $post;
while ($query->have_posts()) {
$query->the_post();
// if posts are returned and more than 7days, update infosubmit field
$diff = time() - strtotime(get_the_date());
if($diff > 604800){
$field_key = "field_60836f942ae12";
$value = "100";
update_field($field_key, $value, $post->ID);
}
} // end while have_posts
wp_reset_postdata();
} // end if have_posts
} // end function
add_action('init','update_active_based_on_date');
How can i get the messages from a certain form?
With this i can get all the inbound messages:
<?php
$args = array(
'post_type' => 'flamingo_inbound',
'post_status' => 'publish'
);
$query = new WP_Query($args);
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
echo '<article>';
echo get_the_title();
echo '</article>';
}
}
wp_reset_postdata();
?>
But i wanna show only the messages from a specific form for the current month. I search on Google and here but i can not find any example or anything for it so i hope somebody knows.
I have made a solution, maybe not the nicest php writing but it works:
<?php
$currentmonth = date('m');
$args = array(
'post_type' => 'flamingo_inbound',
'post_status' => 'publish',
'monthnum' => $currentmonth
);
$query = new WP_Query($args);
if( $query->have_posts() ){
echo '<ul>';
while( $query->have_posts() ){
$query->the_post();
$results = get_post_meta( get_the_ID() );
foreach($results['_meta'] as $result) {
$normaldata = unserialize($result);
if ($normaldata['post_id'] == '1234') { // The id of the post where the form is send from
$title = get_the_title();
echo '<li>' . $title . '</li>';
} else {
}
}
}
echo '</ul>';
}
wp_reset_postdata();
?>
My Wordpress theme allows for the addition of dates to an 'events' custom post type. The stock theme shows all dates entered however I have limited to two dates. Unfortunately it's the first two dates not the next two dates for the selected event.
So as the year passes by, I'd like php to look at the current date and then display forthcoming events ... i.e. the next two event dates from the array.
Here's my code:
<div id="event-days">
<ul>
<?php
global $va_locale;
$days = va_get_the_event_days();
$i = 0;
$len = count($days);
foreach( $days as $date_U => $term ) { ?>
<?php $date = $term->slug; ?>
<?php $display_date = $va_locale->date( apply_filters( 'va_single_event_dates_date_format', get_option( 'date_format' ) ), strtotime( $date ) );?>
<?php $times = va_get_the_event_day_times( $date ); ?>
<li><?php echo html_link( va_event_day_get_term_link( date( 'Y-m-d', strtotime( $date ) ) ), $display_date ); ?><?php echo va_get_the_event_day_time( $times, ' - ', ' # ' );
if ($i == 0) { ?>
<meta itemprop="startDate" content="<?php echo date( 'c', strtotime( $date ) ); ?>" />
<?php } ?>
</li>
<?php $i++;
if($i==2) break;
} ?>
</ul>
</div>
Thanks in advance for your help.
edit - here we go gavriel. cheers
function va_get_the_event_days( $event_id = 0 ) {
$event_id = $event_id ? $event_id : get_the_ID();
$terms = get_the_terms( $event_id, VA_EVENT_DAY );
if ( !$terms )
return array();
$days = array();
foreach ( $terms as $term_id => $term ) {
if ( $term->parent != 0 ) {
$days[ strtotime($term->slug) ] = $term;
}
}
return $days;
}
The easiest change would be to only add the future events to the array:
$now = time();
foreach ( $terms as $term_id => $term ) {
if ( $term->parent != 0 ) {
$term_timestamp = strtotime($term->slug);
if ($term_timestamp > $now) {
$days[ $term_timestamp ] = $term;
}
}
}
I' am learning how to make a WordPress Plugin. I did make few simple plugins but not as complicated as this one. It's a Events Calendar. The var_dump from the function "nc_get_start_date()" on the page it outputs wrong dates.
The output from the var_dump(nc_get_start_date());
string(32) "1970-01-01,1970-01-01,1970-01-01"
This is what the function should return in real
23-12-2013, 25-12-2013, 26-12-2013
In the function.php on the plugin folder. This is the codes
/* Query to get the events post from the database */
function get_nc_events(){
global $post;
$query = new WP_Query(
array(
'post_type' => 'events',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => 'ASC'
)
);
return $query;
}
/* Get the start date from the above function */
function nc_get_start_date(){
$query = get_nc_events();
while ( $query->have_posts() ) : $query->the_post();
$nc_event_id = $post->ID;
$wnc_start_date = get_post_meta( $nc_event_id, 'wnc_start_date');
$wnc_start_date = $wnc_start_date[0];
$wnc_start_date = date("Y-m-d", strtotime($wnc_start_date));
$wnc_start_date_array .= "$wnc_start_date,";
endwhile;
return rtrim($wnc_start_date_array, ",");
}
When I write the code in page-caledar.php without the function it renders everything prefectly.
$query = get_nc_events();
while ( $query->have_posts() ) : $query->the_post();
$nc_event_id = $post->ID;
$wnc_start_date = get_post_meta( $nc_event_id, 'wnc_start_date');
echo $wnc_start_date = $wnc_start_date[0] . "<br/>";
endwhile;
Problem Solved. Thanks everyone. The problem was in this function
/* Get the start date from the above function */
function nc_get_start_date(){
global $post;
$query = get_nc_events();
while ( $query->have_posts() ) : $query->the_post();
$nc_event_id = $post->ID;
$wnc_start_date = get_post_meta( $nc_event_id, 'wnc_start_date');
$wnc_start_date = $wnc_start_date[0];
$wnc_start_date = date("Y-m-d", strtotime($wnc_start_date));
$wnc_start_date_array .= "$wnc_start_date,";
endwhile;
return rtrim($wnc_start_date_array, ",");
}
I didn't make global $post;
I have a Custom post type of "events" and I have a custom metabox for the date of the event. All events added with all dates recorded into database in srtotime() UNIX format.
When list of events is queried I need to separate them by week. So.. "week of 4/23" then list the events for that week only.. followed by "Week of 4/30" with the events for that week listed.
All of this needs to be dynamic and logically detect when each week begins and compare that to the date that is saved in the database for the returned events, thus grouping them into "weeks."
I hope that all makes sense. I am just having trouble finding resources for this type of functionality, and I am uncertain of the logic needed and what WP might already have I could use.
As of now the results are being pulled using a custom Wordpress loop, but I could use SQL if needed.
Thanks in advance for helping any way you can!
*UPDATED CODE
<?php
$loop = new WP_Query(array(
'post_type' => 'tf_events',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'meta_key' => 'tf_events_startdate',
'order' => 'ASC'
));
echo '<pre>';
print_r($loop);
echo '</pre>';
$groups = array();
if ($loop->have_posts()) {
while ($loop->have_posts()) {
$loop->the_post();
$groups['Week of ' . $post->post_date][] = $post;
// Get event dates from WP metabox data (returns as string)
$longstartdate = get_post_meta($post->ID, 'tf_events_startdate', true); //get start date meta data
$longenddate = get_post_meta($post->ID, 'tf_events_enddate', true); //get start date meta data
$longweekof = get_post_meta($post->ID, 'tf_events_weekof', true); //get Week Of date meta data
// Reformat dates
$prettystartdate = date("D. M. j, Y", $longstartdate);
$prettyenddate = date("l, F j, Y", $longenddate);
$prettyweekof = date("m/d", $longweekof);
// Get Custom Meta Terms/Catagories/taxonomies
$the_venue = get_the_term_list( $post->ID, 'venue' );
$event_age = get_the_term_list( $post->ID, 'event_age' );
// Get Custom Metabox Data - URL for Buy Tickets
$buytix = get_post_meta( $post->ID, 'buytix', true );
}
}
?>
<h1>Groups of posts</h1>
<?php echo '<pre>'; print_r($groups); echo '</pre>';?>
<?php foreach ($groups as $week => $rows) : ?>
<h2><?php echo $week ?></h2>
<ul>
<?php foreach ($rows as $post) : setup_postdata($post) ?>
<?php echo ' <h1>POST START</h1> <pre>';
print_r($post);
echo '</pre> <h1>POST END</h1>'; ?>
<li><?php the_title() ?> <?php echo $prettystartdate; ?></li>
<?php endforeach ?>
</ul>
<?php endforeach ?>
When I need to group elements I normally use an associative array where the key is the thing to group by. So in your case the array would look something like:
array(
'Week of 4/30' => array(array of posts)
);
An easy way to do this (once you have all the posts) is like this:
$groups = array();
foreach ($posts as $post) {
$groups['Week of ' . date('m/d', $post->pub_date)][] = $post;
}
And then you can just:
foreach ($groups as $title => $posts) {
echo "<h2>$title</h2>";
foreach ($posts as $post) ...
}
Edit: Here's what a more complete code (including WP query etc) would look:
<?php
# Get all posts
$loop = new WP_Query(array(
'post_type' => 'tf_events',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'meta_key' => 'tf_events_startdate',
'order' => 'ASC'
));
$groups = array();
# Create groups of posts
if ($loop->have_posts) {
while ($loop->have_posts()) {
$loop->the_post();
$groups['Week of ' . date('m/d', $post->tf_events_weekof)][] = $post;
}
}
?>
<h1>Groups of posts</h1>
<?php foreach ($groups as $week => $rows) : ?>
<h2><?php echo $week ?></h2>
<ul>
<?php foreach ($rows as $post) : setup_postdata($post) ?>
<li><?php the_title() ?></li>
<?php endforeach ?>
</ul>
<?php endforeach ?>
Here is rough solution that is working for me.
<?php
$loop = new WP_Query(array(
'post_type' => 'tf_events',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'meta_key' => 'tf_events_startdate',
'order' => 'ASC'
));
$groups = array();
$heading = '';
if ($loop->have_posts()) {
while ($loop->have_posts()) {
$loop->the_post();
//set up pretty week
$weekof = get_post_meta($post->ID, 'tf_events_weekof', true); //get Week Of date meta data
$longstartdate = get_post_meta($post->ID, 'tf_events_startdate', true); //get start date meta data
$longenddate = get_post_meta($post->ID, 'tf_events_enddate', true); //get start date meta data
//format dates
$prettyweekof = date("m/d", $weekof);
$prettystartdate = date("D. M. j, Y", $longstartdate);
$prettyenddate = date("l, F j, Y", $longenddate);
//set info on objects
$post->prettyweekof = $prettyweekof;
$post->pretty_startdate = $prettystartdate;
$post->pretty_enddate = $prettyenddate;
//Echo heading when need to
if($post->prettyweekof !== $heading)
{
$heading = $post->prettyweekof;
echo '<h2>'.$heading.'</h2>';
}
//list events
echo $post->post_title.' | '.$post->pretty_startdate.' - '.$post->pretty_enddate.'<br />';
}
}
?>