Skip to content

Instantly share code, notes, and snippets.

@JiveDig
Last active January 27, 2022 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JiveDig/1505baecd3cfee6dd642f3b0a0509c82 to your computer and use it in GitHub Desktop.
Save JiveDig/1505baecd3cfee6dd642f3b0a0509c82 to your computer and use it in GitHub Desktop.
Convert Mai Theme v1 banner images to v2 page header images.
<?php
/**
* Migrates post and term meta keys from Mai Theme v1 to v2.
*
* 1. Add this code to functions.php
* 2. Visit the front end of the website ONE time.
* 3. This code will run just by visiting the page.
* 4. Each page view or refresh will make it run again so don't do it more than once.
* 5. Immediate remove this code.
* 6. Check your banner image fields to make sure the images are there.
*
* @return void
*/
add_action( 'genesis_before_loop', function() {
/**
* Migrate post meta keys from Mai Theme v1 to v2.
*/
$query = new WP_Query(
[
'post_type' => 'post',
'posts_per_page' => 500,
'offset' => 0,
'post_status' => 'any',
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
]
);
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : $query->the_post();
$banner_id = get_post_meta( get_the_ID(), 'banner_id', true );
if ( $banner_id ) {
update_post_meta( get_the_ID(), 'page_header_image', $banner_id );
}
endwhile;
}
wp_reset_postdata();
/**
* Migrate term meta keys from Mai Theme v1 to v2.
*/
$term_query = new WP_Term_Query(
[
'number' => 500,
'offset' => 0,
'taxonomy' => [
'category',
'post_tag',
],
]
);
if ( ! empty( $term_query->terms ) ) {
/**
* Terms.
*
* @var WP_Term $term Term object.
*/
foreach ( $term_query->terms as $term ) {
$banner_id = get_term_meta( $term->term_id, 'banner_id', true );
if ( $banner_id ) {
update_term_meta( $term->term_id, 'page_header_image', $banner_id );
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment