I have a function where I am counting letters in a title and adding that _product_meta_title key
, everything is working fine when I am adding a new product but I have 500 products that don't have this key. So I need to run my function on all posts once.
function save_post_title_length_meta( $post_id, $post, $update ) {
/*
* In production code, $slug should be set only once in the plugin,
* preferably as a class property, rather than in each function that needs it.
*/
$post_type = get_post_type($post_id);
$title = get_post_field( 'post_name', $post_id );
$title_length = strlen( $title );
// If this isn't a 'product' post, don't update it.
if ( "product" != $post_type ) return;
// - Update the post's metadata.
if ( $title ) {
update_post_meta( $post_id, '_product_title_length', $title_length );
}}
I am using this action to run my function
add_action( 'save_post', 'save_post_title_length_meta', 10, 3 );
How to run it so it will affect all my existing post on wp load, init? Any recommendations on where to start?
Thanks