I have custom post type with some custom fields. And "date" field and "status" field are one of them.
Here am trying to update meta_value of "status" filed based on comparison of "date" field with current date.
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI have custom post type with some custom fields. And "date" field and "status" field are one of them.
Here am trying to update meta_value of "status" filed based on comparison of "date" field with current date.
Since you need to update all the posts in a particular taxonomy, it sounds like you need to read up on how WP_Query()
works.
Here's a simple loop to get you started that will fetch all posts in a taxononmy, and update them if the "now is later than then" condition is met.
$nitish_args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'YOUR TAXONOMY HERE',
),
),
);
$nitish_query = new WP_Query( $nitish_args );
if( $nitish_query->have_posts() ){
// Get "current" unix timestamp
$now = time();
while( $nitish_query->have_posts() ){
$nitish_query->the_post();
// Get "date" meta field as unix timestamp
$then = strtotime( get_post_meta( $post->ID, 'date', true ) );
if( $now > $then ){
// $now is later than $then, update post.
update_post_meta( $post->ID, 'status', 'NEW VALUE' );
}
}
wp_reset_postdata();
}
Sounds like a simple if condition. You can run this on:
is_singular()
or is_single()
functions in functions.php)save_post()
actionIt really depends on your needs, but here's something to get you started:
// Get "date" meta field as unix timestamp
$then = strtotime( get_post_meta( $post_id, 'date', true ) );
// Get "current" unix timestamp
$now = time();
if( $now > $then ){
// $now is later than $then, update post.
update_post_meta( $post_id, 'status', 'NEW VALUE' );
}
Basically you just need to get the unix timestamp of the date
field, compare that to the current unix timestamp with the time()
function, and then see if "now" is greater than "then", and if so - update the post.
WP_Query()
is something you should read up on. The new code sample fetches all posts in that taxonomy, and then compares the date field to the current time and then updates the status field if true.
– Xhynk
Feb 28 '18 at 16:51