U
U
ustrechko2018-09-15 12:50:03
WordPress
ustrechko, 2018-09-15 12:50:03

How to bulk regenerate post_name in wp_posts?

There are thousands of posts on a wordpress site. The id of all posts is different, but it so happened that the value of post_name for many posts turned out to be the same, and from this post_name the url of the post is obtained. For example, 5 entries have post_name = trololo, then if you enter site.ru/trololo.html, all 5 entries will be displayed on this page. Help with a plugin that regenerates this column for trololo, trololo-2, trololo-3, etc., or what query should be performed to the database. Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
id_baton4eg, 2018-09-15
@id_baton4eg

I would implement it something like this:

$posts = get_posts( array(
  'numberposts'      => - 1,
  'post_type'        => 'post',
  'suppress_filters' => true, // подавление работы фильтров изменения SQL запроса
) );

foreach ( $posts as $post ) {
  setup_postdata( $post );
  if ( strpos( $post->name, 'trololo' ) ) {
    $i ++;
    $new_name = $post->name . '-' . $i;
// Создаем массив данных
    $my_post               = array();
    $my_post['ID']         = $post->ID;
    $my_post['post_name'] = $new_name;
  }
// Обновляем данные в БД
  wp_update_post( wp_slash( $my_post ) );
}

wp_reset_postdata(); // сброс

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question