Question

I registered a custom post type "job" to create job board site. Without a plugin, how can I make the posts auto change status to pending after 90 days? I am new in WordPress & PHP. I tried some solutions found on the internet, but not working.

Sorry that I don't really good in PHP, I can't write code myself. For testing, do these solutions need to wait for 1 days if I change the code expired 1 day? Cos I paste the code in my functions.php and nothing happened.

Tried Solutions: Code to auto expire posts after 30 days Automatically move pending posts after 30 days and, update the post date, when users update the posts Set post to draft after set period based on post_modified date Posts to expire (deleted) after a date

Was it helpful?

Solution

This is one of those questions that seem easy enough, but are actually quite complicated. If you don't want to use an existing plugin, you'll have to write your own (and hence learn more php as this is not a please-write-my-code-for-free-site). Here's an outline.

First you will need to select all the posts of type job which are older than 90 days and which have status "published". Like this:

$args = array(
  'post_type' => 'job',
  'date_query' => 'XXX',
  'post_status' => 'published',
  'nopaging' => true );
$wpse251989_query = wp_query ($args);

At XXX you will need to write a query selecting the date 90 days ago (you cannot just subtract 90 from today, because that will get you a negative day if it is not yet april).

Now, you must loop through all posts you retrieve and change the status.

if ( $wpse251989_query->have_posts() ) {
  while ( $wpse251989_query->have_posts() ) {
    $wpse251989_query->the_post();
    wp_update_post(array ('post_status' => 'pending'));
    }
  }

If you dump this code in you functions.php it will be executed on every page load. You could get away with that if you have a powerful server. You could also put it behind a condition like is_admin, making sure it is only executed when an administrator is logged in. The safest way to make sure this is executed only once daily is using a cron job, which brings you into advanced php territory.

OTHER TIPS

Try Below code in your functions.php

function expire_posts() {
  global $wpdb;
  $daystogo = "30";
  $sql = "UPDATE wp_posts SET `post_status` = 'draft' WHERE `post_type` = 'post' AND DATEDIFF(NOW(), `post_date`) > '$daystogo')";
  $wpdb->query( $sql );
}

add_action ('init' ,'expire_posts')
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top