Quantcast
Channel: Topic Tag: MySQL | WordPress.org
Viewing all articles
Browse latest Browse all 5527

Samuel Elh on "Delete posts and related meta older than X years?"

$
0
0

Put this in your functions.php file, make sure to change 2004 in the 22nd line to set the max date you want to list its posts, for 2004, it should list all posts published before or in 2004:

function delete_old_content($max_date) {
	require_once( ABSPATH. "wp-blog-header.php" );
	query_posts('showposts=-1');
	if(have_posts()) {
		while(have_posts()) : the_post();
			$ids = get_the_ID(). ',';
			$list = array( $ids );
			foreach ($list as $items) {
				$date = get_the_date("Y");
				if($date <= $max_date) {
					echo "<strong>".get_the_title(). "</strong> => ".get_the_date("Y"). "<br />";
				}
			}
		endwhile;
		wp_reset_query();
	}
}

function delete_old_content_shortcode() {
	if ( current_user_can( 'manage_options' ) ) {
		ob_start();
		delete_old_content("2004");
		return ob_get_clean();
	}
}
add_shortcode('delete-old-content', 'delete_old_content_shortcode');

After that, add this shortcode [delete-old-content] in a new post/page, save and visit that post/page, and tell me if it is listing the desired posts you want to delete.

Please note that yet we are not deleting anything, just making sure we selected the right targets.


Viewing all articles
Browse latest Browse all 5527

Trending Articles