最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Published post auto change status to pending after 90 days

programmeradmin1浏览0评论

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

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

Share Improve this question edited Nov 6, 2019 at 10:43 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Nov 6, 2019 at 7:34 BryanBryan 55 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

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.

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')
发布评论

评论列表(0)

  1. 暂无评论