Move function to append meta fields into utility

This commit is contained in:
Tetrakern 2024-01-28 15:31:16 +01:00
parent bd8131adb3
commit 7d8de5260c
3 changed files with 50 additions and 46 deletions

View File

@ -1145,7 +1145,7 @@ function fictioneer_paginate_links( $args = [] ) {
*
* @since 5.9.3
*
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance, passed by reference.
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance, passed by reference.
*/
function fictioneer_adminbar_add_chapter_link( $wp_admin_bar ) {

View File

@ -2571,4 +2571,53 @@ function fictioneer_truncate( string $string, int $length, string $ellipsis = nu
}
}
// =============================================================================
// APPEND MISSING META FIELDS
// =============================================================================
/**
* Append a missing meta field to selected posts
*
* @since 5.7.4
* @global wpdb $wpdb WordPress database object.
*
* @param string $post_type The post type to append to.
* @param string $meta_key The meta key to append.
* @param mixed $meta_value The value to assign.
*/
function fictioneer_append_meta_fields( $post_type, $meta_key, $meta_value ) {
global $wpdb;
// Setup
$values = [];
// Get posts with missing meta field
$posts = $wpdb->get_col("
SELECT p.ID
FROM {$wpdb->posts} p
LEFT JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id AND pm.meta_key = '{$meta_key}'
WHERE p.post_type = '{$post_type}' AND pm.meta_id IS NULL
");
// Prepare values
foreach ( $posts as $post_id ) {
$values[] = $wpdb->prepare( "(%d, %s, %d)", $post_id, $meta_key, $meta_value );
}
$chunks = array_chunk( $values, 1000 );
// Query
foreach ( $chunks as $chunk ) {
$values_sql = implode( ', ', $chunk );
if( ! empty( $values_sql ) ) {
$wpdb->query("
INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value)
VALUES {$values_sql};
");
}
}
}
?>

View File

@ -1196,51 +1196,6 @@ function fictioneer_tools_add_chapter_hidden_fields() {
}
add_action( 'admin_post_fictioneer_tools_add_chapter_hidden_fields', 'fictioneer_tools_add_chapter_hidden_fields' );
/**
* Append a missing meta field to selected posts
*
* @since 5.7.4
* @global wpdb $wpdb WordPress database object.
*
* @param string $post_type The post type to append to.
* @param string $meta_key The meta key to append.
* @param mixed $meta_value The value to assign.
*/
function fictioneer_append_meta_fields( $post_type, $meta_key, $meta_value ) {
global $wpdb;
// Setup
$values = [];
// Get posts with missing meta field
$posts = $wpdb->get_col("
SELECT p.ID
FROM {$wpdb->posts} p
LEFT JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id AND pm.meta_key = '{$meta_key}'
WHERE p.post_type = '{$post_type}' AND pm.meta_id IS NULL
");
// Prepare values
foreach ( $posts as $post_id ) {
$values[] = $wpdb->prepare( "(%d, %s, %d)", $post_id, $meta_key, $meta_value );
}
$chunks = array_chunk( $values, 1000 );
// Query
foreach ( $chunks as $chunk ) {
$values_sql = implode( ', ', $chunk );
if( ! empty( $values_sql ) ) {
$wpdb->query("
INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value)
VALUES {$values_sql};
");
}
}
}
// =============================================================================
// MIGRATION TOOLS ACTIONS
// =============================================================================