December 30, 2021

Modification Tools

Use Modification Tools to alter the existing WordPress posts.

Re-synonymize post titles

Enable this option to re-synonymize the titles of all existing posts. The Global Synonym Table will be used.

Re-synonymize post contents

Enable this option to re-synonymize the contents of all existing posts. The Global Synonym Table will be used.

Re-synonymize post descriptions

Enable this option to re-synonymize the descriptions of all existing posts. The Global Synonym Table will be used.

Generate missing post thumbnails

Enable this option to regenerate the missing post thumbnails.

PHP Code <?php .. ?>

You can use this form to execute lines of PHP code for every existing post in your blog’s database. All post-related info is stored in the $post object variable which you can alter in order to apply your own changes to every post.

The following variables are defined in the $post object:

  • $post->ID – post ID;
  • $post->post_author – post author’s numerical ID;
  • $post->post_date – post date in text format, e.g. “1939-09-01”;
  • $post->post_date_gmt – post GMT date in text format;
  • $post->post_title – post title;
  • $post->post_content – post content;
  • $post->post_excerpt – post excerpt (description);
  • $post->post_status – post status (publish, future, draft, pending, private etc);
  • $post->comment_status – post comments status (open, closed);
  • $post->ping_status – (open, closed);
  • $post->post_password – post password;
  • $post->post_name – post name (slug);
  • $post->to_ping – list of URLs to ping;
  • $post->pinged – list of URLs that have been pinged (for published posts);
  • $post->post_modified – date the post was last modified;
  • $post->post_modified_gmt – GMT date the post was last modified;
  • $post->post_content_filtered – exists to store a cached version of post content with the_content filters already applied;
  • $post->post_parent – ID of this post’s parent. In the case of attachments, will be the post it’s attached to;
  • $post->guid – post GUID (Global Unique Identifier). The same as the URL to the post, not the permalink version. For pages, this is the actual URL. In the case of files (attachments), this holds the URL to the file;
  • $post->menu_order – values for display order of posts and pages (1, 2, 3 etc);
  • $post->post_type – returns the type (post or page);
  • $post->post_mime_type – only used for files (attachments) and contains the MIME type of the uploaded file;
  • $post->comment_count – number of comments, pings, and trackbacks combined;

Alter these variables to modify all the existing posts. If you don’t want to modify some particular post, assign FALSE to the $post object ($post = false;)

Examples

The following script removes all posts that don’t have a featured image.

$thumb_id = get_post_meta($post->ID, '_thumbnail_id', true);
if ($thumb_id == '') {
    wp_delete_post($post->ID, true);
}

The following script removes all images containing a specified keyword in the URL (‘cyberseo.net/’) from post bodies (post_content) and post descriptions (post_excerpt).

$keyword = 'cyberseo.net/';
if (stripos($post->post_content . $post->post_excerpt, $keyword) !== false) {
    $post->post_content = preg_replace('/<img.+?src=.*?' . preg_quote($keyword, '/') . '.*?>/is', '', $post->post_content);
    $post->post_excerpt = preg_replace('/<img.+?src=.*?' . preg_quote($keyword, '/') . '.*?>/is', '', $post->post_excerpt);
} else {
    $post = false;
}

The following script adds ” [obsolete]” to the title of every post, which was created before 28th of December 2021.

$date = '2021-12-28';
if (strtotime($post->post_date) <= strtotime($date)) {
    $post->post_title .= ' [obsolete]';
} else {
    $post = false;
}

The following script deletes all posts that were created before 20th of January 2021. If $bypass_trash is true, the posts will be deleted permanently. If false, they will be sent to trash.

$date = '2021-01-20';
$bypass_trash = true;
if (strtotime($post->post_date) < strtotime($date)) {
    wp_delete_post($post->ID, $bypass_trash);
}

The following script deletes every post that contains “COVID19” in its title, content or description. If $bypass_trash is true, the posts will be deleted permanently. If false, they will be sent to trash.

$keyword = 'COVID19';
$bypass_trash = true;
if (strpos($keyword, $post->post_title . $post->post_content . $post->post_excerpt) !== false) {
    wp_delete_post($post->ID, $bypass_trash);
}

Yet another code which deletes all posts that older than year and have less than 2 views, counted by WP-PostViews plugin.

$date = time() - 365 * 24 * 60 * 60;
$bypass_trash = true;
if ($post->post_type == 'post' && get_post_meta($post->ID, 'views', true) < 2 && strtotime($post->post_date) < $date) {
    wp_delete_post($post->ID, $bypass_trash);
}

You don’t need to install any 3rd-party plugins to enable or disable comments for posts or pages. For example this code enables comments for all existing posts:

if ($post->post_type == 'post') {
    $post->comment_status = 'open';
} else {
    $post = false;
}

This code disables comments in all posts tagged as “politics”.

if ($post->post_type == 'post' && in_array('politics', get_the_tags($post->ID))) {
    $post->comment_status = 'closed';
} else {
    $post = false;
}