How to Automatically change a Post's Status in WordPress

Congrats, You are Subscribed to Receive Updates.

How to Automatically Change a Post’s Status in WordPress

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');
function my_hourly_event() {
	$the_query = get_posts( 'post_type=job' );	
	foreach($the_query as $single_post) {
		$ad_close_date=get_post_meta($id, 'ad_close_date', true );
				$update_post = array(
				'ID' 			=> $id,
				'post_status'	=>	'private',
				'post_type'	=>	'job' );

The above one will change the post status, when the closing date is less than today.



About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, PHP and Data Analyst. Database Management Advance Level


  1. commenter

    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
    3pm: published — 10pm: draft

    3pm: published — 830pm: draft

    Do you think this can be done? Thanks for any guidance…

    • commenter

      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.

  2. commenter

    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

Comment Below

Your email address will not be published. Required fields are marked *


Current ye@r *



Subscribe E-mail Updates

Congrats, You are Subscribed to Receive Updates.


Category Posts