I have the following code in a single script to be run one time to fix post names which are empty.
// WP_Query arguments
$args = array (
'post_type' => 'books',
'name' => '',
'post_status' => 'publish',
'posts_per_page' => -1,
);
add_filter( 'posts_where', 'custom_posts_where' );
echo "1";
// The Query
$query = new WP_Query( $args );
echo "2";
$germanizer = new Germanizer();
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// do something
$seo_slug = seo_slugs( $germanizer->sanitize_title_filter( $post->post_title ) );
// check the slug and run an update if necessary
if ( $post->post_name != $seo_slug ) {
wp_update_post(
array (
'ID' => $post->ID,
'post_name' => $seo_slug
)
);
}
}
}
function custom_posts_where( $where ) {
remove_filter( current_filter(), __FUNCTION__ );
if( FALSE === strpos( $where, 'post_name' ) ) {
$where .= sprintf( " AND %s.post_name = '' ", $GLOBALS['wpdb']->posts );
}
return $where;
}
// Restore original Post Data
wp_reset_postdata();
I have included wp-config.php
so that is not the problem. In fact the script executes without any error on my local development environment (MAMP) and prints 2
but does not update the post_name
.
But on my test env, it echoes only 1
without any error and it naturally does not update the post_name
. The total number of posts on my test env is around 5k, so I wonder if that is the reason.
This code uses Germanix Plugin by @toscho and might be relevant information.