How to Automatically Change a Post’s Status in WordPress |
X

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');
//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.

 

commenter

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

5 comments

  1. commenter

    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…

    • 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

  3. commenter

    This is very helpful. I have a question on how I would modify it for my needs.
    I want to change the post status on specific posts that are in a specific category. And maybe be able to run this once a day, possibly in a cron job.

    Any thoughts?
    Thanks for your helpful code.

    • commenter

      You can query the posts additionally with category or tags and than run it. ofcourse without cron you can do the daily, twicedaily, hourly, check the wp_schedule_event more on wordpress to get more changes as per your post type changes

Reply to Varadharaj V Cancel reply

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

*

Current ye@r *

Menu

Sidebar