Skip to content

Instantly share code, notes, and snippets.

@mjangda
Created November 2, 2011 22:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mjangda/1335091 to your computer and use it in GitHub Desktop.
Save mjangda/1335091 to your computer and use it in GitHub Desktop.
Simple generic command line script for WordPress
<?php
// Let's load WordPress
require( 'wp-load.php' );
if ( ! function_exists( 'wp' ) )
die( 'Sorry, looks like WordPress isn\'t loaded.' );
// Run the core code for our script
my_command_line_script();
function my_command_line_script() {
// We're going to run through batches of 50 posts at a time
$posts_per_page = 50;
// Set up the main query args
$args = array(
'posts_per_page' => $posts_per_page,
'offset' => 0,
);
// Let's get our first batch
$query = new WP_Query( $args );
// Now let's loop through out posts until we've gone through all of them
while ( $query->have_posts() ) {
$query->the_post();
// Our main script actions would go here...
// Let's get the next batch
$args['offset'] += $posts_per_page;
$query = new WP_Query( $args );
}
}
@ismailovic95
Copy link

hello,
how can I execute this script via command line ?

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