Skip to content

Instantly share code, notes, and snippets.

@mgratch
Created May 18, 2023 17:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgratch/1971033a442302ce465ca8ca2d8d87c3 to your computer and use it in GitHub Desktop.
Save mgratch/1971033a442302ce465ca8ca2d8d87c3 to your computer and use it in GitHub Desktop.
Use WP-CLI to enter `wp shell`, enter the following class, and then run `(new SEO_Update_Command())->__invoke()` to start the process.
if (class_exists('WP_CLI')) { class SEO_Update_Command { public function __invoke() { global $wpdb; $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'salary_market_data' AND post_parent != 0"); $progress = \WP_CLI\Utils\make_progress_bar( 'Updating...', count($post_ids)); foreach($post_ids as $post_id) { update_post_meta($post_id, "_yoast_wpseo_meta-robots-noindex", 1); $progress->tick(); } $progress->finish(); WP_CLI::success('Meta values updated successfully.'); }} WP_CLI::add_command('seo_update', 'SEO_Update_Command');}
@mgratch
Copy link
Author

mgratch commented Oct 20, 2023

The following command will update for a specific subset of Title.

if (class_exists('WP_CLI')) { class SEO_Update_Command { public function __invoke() { global $wpdb; $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_parent in (SELECT ID FROM $wpdb->posts WHERE post_type = 'salary_market_data' AND post_parent = 0 AND post_title in ('BIM Modeler', 'CRM specialist', 'mortgage underwriter', 'rust developer', 'business analyst'))"); $progress = \WP_CLI\Utils\make_progress_bar('Updating...', count($post_ids)); foreach ($post_ids as $post_id) { update_post_meta($post_id, "_yoast_wpseo_meta-robots-nofollow", 1); update_post_meta($post_id, "_yoast_wpseo_meta-robots-noindex", 1); $progress->tick(); } $progress->finish(); WP_CLI::success('Meta values updated successfully.'); } } WP_CLI::add_command('seo_update', 'SEO_Update_Command'); }

Knowing the value of the options is important

		/* translators: %1$s expands to Yes or No,  %2$s expands to the post type name.*/
		WPSEO_Meta::$meta_fields['advanced']['meta-robots-noindex']['options']['0'] = __( 'Default for %2$s, currently: %1$s', 'wordpress-seo' );
		WPSEO_Meta::$meta_fields['advanced']['meta-robots-noindex']['options']['2'] = __( 'Yes', 'wordpress-seo' );
		WPSEO_Meta::$meta_fields['advanced']['meta-robots-noindex']['options']['1'] = __( 'No', 'wordpress-seo' );

		/* translators: %1$s expands to the post type name.*/
		WPSEO_Meta::$meta_fields['advanced']['meta-robots-nofollow']['title']        = __( 'Should search engines follow links on this %1$s?', 'wordpress-seo' );
		WPSEO_Meta::$meta_fields['advanced']['meta-robots-nofollow']['options']['0'] = __( 'Yes', 'wordpress-seo' );
		WPSEO_Meta::$meta_fields['advanced']['meta-robots-nofollow']['options']['1'] = __( 'No', 'wordpress-seo' );

@cdils
Copy link

cdils commented Mar 18, 2024

For easy future reference, here's the updated SQL to set all jobs CPT to both noindex and nofollow:

if (class_exists('WP_CLI')) { class SEO_Update_Command { public function __invoke() { global $wpdb; $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'jobs'"); $progress = \WP_CLI\Utils\make_progress_bar( 'Updating...', count($post_ids)); foreach($post_ids as $post_id) { update_post_meta($post_id, "_yoast_wpseo_meta-robots-nofollow", 1); update_post_meta($post_id, "_yoast_wpseo_meta-robots-noindex", 1); $progress->tick(); } $progress->finish(); WP_CLI::success('Meta values updated successfully.'); }} WP_CLI::add_command('seo_update', 'SEO_Update_Command');}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment