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');
Related
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();
My posts get ordered by date which is picked by an advanced custom field datepicker. I want to use the regular Wordpress function references [the_title(), etc …] and the post related custom field's.
Right now the output of every loop is the the same. I read setup_postdata() can solve this issue and enable the use of the regular function references. I tried to apply it, but the output keeps being always the same. Thanks
<?php
global $posts;
$posts = get_posts(array(
'post_type' => 'post',
'meta_key' => 'release_date',
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
$group_posts = array();
if( $posts ) {
foreach( $posts as $post ) {
$date = get_field('release_date', $post->ID, false);
$date = new DateTime($date);
$year = $date->format('Y');
$month = $date->format('F');
$group_posts[$year][$month][] = array($post, $date);
}
}
foreach ($group_posts as $yearKey => $years) {
foreach ($years as $monthKey => $months) {
echo '<li class="time">' . $monthKey . ' ' . $yearKey . '</li>';
foreach ($months as $postKey => $posts) {
setup_postdata($posts); ?>
<li class="item clearfix">
<!-- Wordpress Functions -->
<?php the_title();?>
<?php the_permalink();?>
<!-- Advanced Custom Fields -->
<?php the_field('blabla')?>
</li>
<?php
}
}
} wp_reset_postdata();
?>
You just need to add a line $post = $current_post; just before calling setup_postdata( $post ). See my example below to have a clear idea:
$posts = get_posts(array(.......));
// Call global $post variable
global $post;
// Loop through sorted posts and display using template tags
foreach( $posts as $current_post ) :
//the below line is what you missed!
$post = $current_post; // Set $post global variable to the current post object
setup_postdata( $post ); // Set up "environment" for template tags
// Use template tags normally
the_title();
the_post_thumbnail( 'featured-image-tiny' );
the_excerpt();
endforeach;
wp_reset_postdata();
For details please see this comment posted in the WP-Codex;
I was hoping someone could help me with this problem.
I'm trying to make a loop in wordpress with two different post types ('product' and 'outfits')
The code below is working fine, this outputs a list of the two post types ordered by newest to older.
$loop = new WP_Query( array(
'post_type' => array( 'product', 'outfits' ),
'posts_per_page' => 15
) );
$counter = 0;
?>
<?php if ( $loop->have_posts() ) { while ( $loop->have_posts() ) { $loop->the_post();
$counter++; ?>
<?php $loop->is_home = false; ?>
<?php
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
$titulo = get_the_title();
$content = apply_filters ('the_content', $post->post_content); ?>
<div class="caja-<?php echo $counter; ?>">
<?php if ( $post->post_type == "product" ) {
//some stuff
<?php } else {
//some stuff
} ?>
</div>
<?php } } wp_reset_postdata();
What I would like to do is to insert a product post type after X number of outfits.
I was trying to merge a similar solution I've found in other question with no luck. I'm not a php expert so any help is appreciated
<?php
$args = array('post_type'=>'post', 'posts_per_page'=>9, 'category_name'=>'news');
$posts = get_posts($args);
$args = array('post_type'=>'testimonials', 'posts_per_page'=>3);
$testimonials = get_posts($args);
// see how many of the regular posts you got back //
$post_count = count($posts);
// see how many testimonials you got back //
$testimonial_count = count($testimonials);
// add them up to get the total result count //
$total_count = $post_count + $testimonial_count;
// Loop through the total number of results //
for($i = 1; $i <= $total_count; $i++){
// assuming you want to show one testimonial every third post //
if($i % 3 == 0){
// this means you're on the a third post, show a testimonial //
setup_postdata($testimonials[$i]);
}
else{
/** show a regular post */
setup_postdata($posts[$i]);
}
/** and now handle the output */
?><h1><?php the_title();?></h1><?php
} ?>
$x = 3;
$products = get_posts(array(
'post_type' => 'product',
'posts_per_page' => -1
));
$outfits = get_posts(array(
'post_type' => 'outfit',
'posts_per_page' => -1
));
foreach ($outfits as $num => $outfit) {
if ( ($num+1) % $x == 0) {
if (isset($products[($num+1)/$x - 1])) {
array_splice( $outfits, $num, 0, array($products[($num+1)/$x - 1]) );
}
}
}
foreach ($outfits as $outfit) {
$posts_id[] = $outfit->ID;
}
$query = new wp_query(array(
'post_type' => array('outfit', 'product'),
'post__in' => $posts_id,
'posts_per_page' => 15,
'orderby' => 'post__in'
));
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'm using "The Future Is Now!" plugin and want to display all post that are schedule for the future. The only problem is, how do i make a query like (WHERE date >= $currentdate) before i enter the loop?
<?php if (is_category('My awesome category')) {
$currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
/* Some sort of query with the statement date >= $currentdate? */
}
?>
/* The Loop */
<?php if (have_posts()) : while (have_posts()) : the_post();
?>
query_posts(array('post_status' => 'future'));
Edit: Above is the easy answer that fits with your loop, but as a default solution it's better that u use a new $WP_Query object:
$my_query = new $WP_Query;
$my_query->query_posts(array('post_status' => 'future'));
while ($my_query->have_posts()) :
$my_query->the_post();
the_title();
endwhile;
wp_reset_postdata(); // now the main wordpress query and post data is intact
2nd Edit: Similar query but with a filter:
function filter_where( $where = '' ) {
// posts in the future
$now = date("Y-m-d H:i:s");
$where .= " AND post_date >= '$now'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$q = new WP_Query(array('post_type' => 'post'));
while ($q->have_posts()) :
$q->the_post();
the_title();
endwhile;
remove_filter( 'posts_where', 'filter_where' );