How to Automatically Change a Post’s Status in WordPress
- Article
- Comment (3)
How to Automatically change a Post’s Status in WordPress. Changing a post status needs to find a post and change it based on certain attribute. Here i am considering the post as job post. which has start date and close date. After the close date we have to change the post status to hide it from users view. The following function will help you to set it private.
wp_schedule_event(time(), 'daily', 'my_hourly_event'); //add_action('init','my_hourly_event'); function my_hourly_event() { $the_query = get_posts( 'post_type=job' ); foreach($the_query as $single_post) { $id=$single_post->ID; $ad_close_date=get_post_meta($id, 'ad_close_date', true ); if($ad_close_date!=''){ $today=date("Y-m-d"); if($ad_close_date<$today){ $update_post = array( 'ID' => $id, 'post_status' => 'private', 'post_type' => 'job' ); wp_update_post($update_post); } } } }
The above one will change the post status, when the closing date is less than today.
HI,
I’ve been looking all over, and I think your code is the closest to what I want to do. Essentially. I’d like to publish and unpublish ONE specific post during specific times during the week. EX:
POST ID: 4705
Monday-Friday
3pm: published — 10pm: draft
Saturday/Sunday
3pm: published — 830pm: draft
Do you think this can be done? Thanks for any guidance…
You have to schedule it twice. once for the draft and you have to set the time gap for the 3pm – 10pm, than publish it again. monday to friday.
Alternative thing also you can code.
Hi, Code is good but I have a question –
How to auto-update published post date of CPT Company if new post published in CPT Jobs. There is a common custom Meta Field and Value in Jobs and Company post. So when a job post’s is published so update related company post’s at the same time.
What should be the code? Thank You