database – Bulk set Post Title as Tag where Tag is Empty Function

Question

I am using the following function to set the Title as Tag for each of my published posts. Unfortunately it does not work with over 20K+ Pages -> Timeout.

My solution would be limiting the posts_per_page to 1000 and run the query multiple times on posts where the tag is empty.

Any idea how to check if the tag is empty? Or any other idea how to fix this issue. THX

add_action( 'init', 'default_function' );
function default_function() {

    if ( is_admin() ) { 
    
        $args = array(
            'posts_per_page'   => 1000,
            'offset'           => 0,
            'category'         => '',
            'category_name'    => '',
            'orderby'          => 'date',
            'order'            => 'DESC',
            'include'          => '',
            'exclude'          => '',
            'meta_key'         => '',
            'meta_value'       => '',
            'post_type'        => 'post',
            'post_mime_type'   => '',
            'post_parent'      => '',
            'author'       => '',
            'author_name'      => '',
            'post_status'      => 'publish',
            'suppress_filters' => true
        );
        $posts_array = get_posts( $args );

        foreach ($posts_array as $current_post) {

            $post_id = $current_post->ID;
            $post_title = $current_post->post_title;

            wp_set_post_tags( $post_id, $post_title, true );

        }
    }
}

0
eva last 4 weeks 2022-11-05T07:03:32-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse