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, OpenCart Theme Development, PHP, HTML, CSS, jQuery, Bootstrap development and content SEO.


  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.

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