/**
* Run queries for posts and assign our term to them.
*
* Conditionally able to run on just one-to-many post types, or all.
*
* @since 1.0.0
*
* @param mixed $chosen Individual post type to potentially use.
*/
function pluginize_backfill_posts_by_post_type( $chosen = '' ) {
// We only want to run this if there is a $_GET param of `?fill_posts=true` or `&fill_posts=true`.
if ( empty( $_GET ) ) {
return;
}
if ( ! isset( $_GET['fill_posts'] ) || 'true' !== $_GET['fill_posts'] ) {
return;
}
if ( ! empty( $chosen ) && is_string( $chosen ) && post_type_exists( $chosen ) ) { // If we are passed a string as a chosen post type and it's valid.
$post_types = [ $chosen ];
} elseif ( is_array( $chosen ) ) { // If we are passed an array of post types.
$post_types = [];
foreach ( $chosen as $item ) {
if ( post_type_exists( $item ) ) { // If we have valid post types.
$post_types[] = $item;
}
}
} else { // Nothing at all passed in, default to all of them.
$post_types = cptui_get_post_type_slugs();
}
$args = [
'post_type' => $post_types,
'post_status' => 'publish',
'fields' => 'ids',
'posts_per_page' => -1,
];
$backfill = new WP_Query( $args ); // We are only needing our post IDs, from published posts.
while( $backfill->have_posts() ) {
$backfill->the_post();
// We can reuse our function from above!
pluginize_auto_add_taxonomy_terms_on_publish( get_the_ID() );
}
}
add_action( 'init', 'pluginize_backfill_posts_by_post_type' );