Skip to content

Instantly share code, notes, and snippets.

@seriusokhatsky
Created February 1, 2017 07:55
Show Gist options
  • Save seriusokhatsky/b0303b52f2dd354177d4a497671a7ba0 to your computer and use it in GitHub Desktop.
Save seriusokhatsky/b0303b52f2dd354177d4a497671a7ba0 to your computer and use it in GitHub Desktop.
Randomly update posts content in database WP
<?php
/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* @package WordPress
*/
/**
* Tells WordPress to load the WordPress theme and output it.
*
* @var bool
*/
define('WP_USE_THEMES', false);
/** Loads the WordPress Environment and Template */
require_once( dirname(__FILE__) . '/wp-load.php' );
function update_descriptions( $descriptions, $except = array() ) {
global $wpdb;
if( empty( $descriptions ) ) return;
$q = "
SELECT ID
FROM $wpdb->posts
WHERE post_type = 'product'
";
if( ! empty( $except ) ) {
$q .= "AND ID NOT IN( " .implode(',', $except) . " )";
}
echo $q . '<br>';
$products = $wpdb->get_results( $q );
if( empty( $products ) ) return;
$_i = 0;
$_num = count( $descriptions );
foreach ( $products as $product ) {
echo $product->ID . '<br>';
$wpdb->update(
$wpdb->posts,
array(
'post_content' => $descriptions[ $_i ]
),
array( 'ID' => $product->ID ),
array(
'%s'
),
array( '%d' )
);
$_i++;
if( $_i >= $_num ) $_i = 0;
}
}
$descriptions = array(
'second',
'fourth',
'first',
'fifth'
);
$exceptions = array( 15, 37, 53 );
update_descriptions( $descriptions, $exceptions );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment