diff --git a/metaupdater.php b/metaupdater.php index 9f0f68a..82221a2 100644 --- a/metaupdater.php +++ b/metaupdater.php @@ -33,65 +33,70 @@ function as_metaupdater_page() { "; - print_r( $data ); - echo ""; - $header_row = array_shift( $data ); - $search_field = $header_row[0]; - $replace_field = $header_row[1]; + // Get header row + list( $search_field, $replace_field ) = array_shift( $data ); - print_r( $header_row ); - print_r( $data ); - - echo "Search: $search_field, replace: $replace_field"; echo "

Results

"; $success = 0; + $not_found = []; foreach( $data as $row ) { + list( $key, $value ) = $row; + + if ( $key == '' ) continue; // skip blank lines + $args = array( 'meta_key' => $search_field, 'post_type' => 'site' ); - if ( is_string( $row[0] ) ) { - $args['meta_value'] = $row[0]; - } elseif ( is_numeric( $row[0] ) ) { - $args['meta_value_num'] = $row[0]; - } + // if ( is_string( $key ) ) { + $args['meta_value'] = $key; + // } elseif ( is_numeric( $key ) ) { + // $args['meta_value_num'] = $key; + // } echo "

"; - print_r( $args ); $query = new WP_Query( $args ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); - echo get_the_title(); - if ( $_POST['dry_run'] != '1' ) { - echo "Updating meta for real"; - if ( update_post_meta( get_the_ID(), $replace_field, $row[1] ) ) { + if ( update_post_meta( get_the_ID(), $replace_field, trim( $value ) ) === true ) { + echo "

";
+              print_r( $args );
+              echo sprintf( "ID: %d
%s: %s
Title: %s
%s: %s", get_the_ID(), $search_field, $key, get_the_title(), $replace_field, $value ); $success++; } } - } } else { + $not_found[]= $key; echo "No results
"; } - } - echo "

Saved $success terms.

"; - + echo "

Updated $success custom fields OK.


"; + + echo "

Errors

"; + if ( count( $not_found) > 0 ) { + echo "

Posts with the custom field $search_field with these values could not be found:

\n"; + } else { + echo "

None.

"; + } } ?> @@ -105,6 +110,7 @@ function as_metaupdater_page() { openlylocal_id,area_covered 2192,"All of London" 2191,"Greater Manchester" +99999,dummy