Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

After switching themes, delete all pm_pattern posts #62

Merged
merged 7 commits into from
Feb 21, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions wp-modules/pattern-post-type/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace PatternManager\PatternPostType;

use WP_Post;
use WP_Query;
use function PatternManager\PatternDataHandlers\get_pattern_by_name;
use function PatternManager\PatternDataHandlers\get_theme_patterns;
use function PatternManager\PatternDataHandlers\delete_pattern;
Expand Down Expand Up @@ -238,3 +239,27 @@ function redirect_pattern_actions() {
}
}
add_action( 'admin_init', __NAMESPACE__ . '\redirect_pattern_actions' );

function get_pm_post_ids() {
Copy link
Contributor Author

@kienstra kienstra Feb 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a separate function so we don't have to have a single query with a 'posts_per_page' that might not get all of the posts.

We can keep calling this in the while loop until there aren't any more posts.

return ( new WP_Query(
[
'post_type' => 'pm_pattern',
'post_status' => 'any',
'fields' => 'ids',
'posts_per_page' => 10,
]
) )->posts;
}

function delete_pattern_posts() {
$post_ids = get_pm_post_ids();

while( ! empty( $post_ids ) ) {
foreach ( $post_ids as $post_id ) {
wp_delete_post( $post_id, true );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 2nd true argument bypasses putting the post in the trash.

}

$post_ids = get_pm_post_ids();
}
}
add_action( 'after_switch_theme', __NAMESPACE__ . '\delete_pattern_posts' );