Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action('init', function () {
- $q = new WP_Query([
- 'post_type' => 'shop_post',
- 'post_status' => 'publish'
- ]);
- function toTimestamp($post) {
- return date_create_from_format('Y-m-d H:i:s', $post->post_date)->getTimestamp();
- }
- remove_action('transition_post_status', 'switch_published_post', 10);
- {
- Ginq::from($q->get_posts())
- ->groupBy(function ($p) { return $p->post_author; }) // grouping posts by author
- ->where(function ($g) { return $g->count() > 1; }) // pick groups whose author wrote more than 1 post
- ->select(function ($g) { return $g->orderByDesc(function ($p) { return toTimestamp($p); }); }) // sort by timestamp
- ->select(function ($g) { return $g->drop(1)->toList(); }) // other than latest
- ->selectMany(function ($p) { return $p; }) // flatten
- ->select(function ($p) { return ['ID' => $p->ID, 'post_status' => 'draft']; }) // to draft
- ->all(function ($a) { return wp_update_post($a); }); // update them
- }
- add_action('transition_post_status', 'switch_published_post', 10, 3);
- });
- function switch_published_post($new_status, $old_status, $post) {
- if ($old_status == 'publish' || $new_status != 'publish') return;
- $q = new WP_Query([
- 'post_type' => 'shop_post',
- 'post_status' => 'publish',
- 'author' => $post->post_author,
- ]);
- remove_action('transition_post_status', 'switch_published_post', 10);
- {
- Ginq::from($q->get_posts())
- ->where(function ($p) use ($post) { return $p->ID != $post->ID; })
- ->select(function ($p) { return ['ID' => $p->ID, 'post_status' => 'draft']; })
- ->all(function ($a) { return wp_update_post($a); });
- }
- add_action('transition_post_status', 'switch_published_post', 10, 3);
- }
- add_action('transition_post_status', 'switch_published_post', 10, 3);
Add Comment
Please, Sign In to add comment