Skip to content

Instantly share code, notes, and snippets.

@rochow
Created March 19, 2018 23:31
Show Gist options
  • Save rochow/6a4273a723698765109912e971175714 to your computer and use it in GitHub Desktop.
Save rochow/6a4273a723698765109912e971175714 to your computer and use it in GitHub Desktop.
WordPress - Set no index on a bulk list of URLs
<?php
function noindex_urls() {
global $wpdb;
$debug = true;
$urls = array(
'http://example.com/ok',
'http://example.com/blah/?g=ok',
'http://example.com/blah/foo/'
);
$new_urls = array();
foreach( $urls as $url ) {
// Only get the actual permalink, remove any query strings
$url = explode( '?', $url );
// We only want the end part of the URL
$url_parts = explode( '/', $url[0] );
$part = $url_parts[ count( $url_parts ) - 1 ];
if( empty( $part ) ) {
$part = $url_parts[ count( $url_parts ) - 2 ];
}
// Get the post ID from the database that matches our URL part
$id = $wpdb->get_var("SELECT ID from $wpdb->posts WHERE post_name='$part'");
// Get the permalink for the post
$permalink = get_permalink( $id );
// If our URL matches the permalink of the post we found, then set Yoast noindex to true
if( $url[0] == $permalink ) {
$new_urls[ $id ] = $url[0];
update_post_meta( $id, '_yoast_wpseo_meta-robots-noindex', 1 );
} else {
if( $debug ) echo $url[0] . ' - ' . $permalink . ' dont match<br>';
}
}
// A print of the URLs that were updated
if( $debug ) {
echo "<pre>";
print_r( $new_urls );
echo "</pre>";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment