Add SKU to all products (for empty, TMP-{ID} will be used)

Add the following code to your functions.php. Go to any page and add ?start=ja after your site URL

add_filter('the_content','aanpassing');
function aanpassing($content){
if ($_GET['start']=="ja"){
  echo 'start';
$query_args = array(
'post_type' => ['product', 'product_variation'],
  'posts_per_page' => -1,


);

// The Query
$the_query = new WP_Query( $query_args );

// The Loop
if ( $the_query->have_posts() ) {
	while ( $the_query->have_posts() ) {
		$the_query->the_post();
      	$sku = get_post_meta( get_the_ID(), '_sku', true );
      	if (empty($sku)){
          echo get_the_ID() .' '. get_the_title().' '. get_post_meta( get_the_ID(), '_sku', true ) .'<br>';
          update_post_meta(get_the_ID(),'_sku','TMP-'.get_the_ID());
        }
      
	}
	/* Restore original Post Data */
	wp_reset_postdata();
} else {
	// no posts found
}
}
  return $content;
}

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest
Author picture

SitiWeb is de expert op het gebied van WordPress. gespecialiseerd in het leveren van maatwerk.

Leave a comment

Geef een reactie

Je e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *