Sambath DreamHigh Sambath DreamHigh - 3 months ago 19
PHP Question

I want to use post title as tag in wordpress. Is there anyway to automate the process instead of copy and paste one by one?

I have 1k of Wordpress posts, I want to use post title as tag. Is there anyway to automate the process instead of copy and paste one by one?

Answer

use below code

add_action( 'init', 'default_function' );

function default_function() {

$args = array(
    'posts_per_page'   => -1,
    '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 );

    }


}

Add above code in your functions.php file

please make sure you only need to execute this once and then remove it from your functions.php file. However i have set 'true' in wp_set_post_tags as third parameter which will append the tags for particular post.

Comments