AnswerBun.com

SQL / wp_update_post: change post custom field to CPT post taxonomy

the setup is as follow:
I have posts with a custom field: actor = 'tom hanks'
for advanced management I created a custom post type (actors) and added “tom hanks” as a post within that actors taxonomy.

Via plugin ‘Advanced custom fields’ I connected the custom taxonomy (actors) to the default posts so I can select ‘tom hanks’ on the regular post page.

This can be done manually (check the custom field value and click the according actors category) but there are way more than 1500 posts and many other similar custom field values.

Do you have any idea how that can be solved via SQL query or function?

in one line:
get value from customfield ‘actors’ and move to taxonomy(‘actors’) for that post.
the custom field value could be used as the taxonomy slug.

I think it’s also possible with the wp_update_post function but I have no idea how to call all posts, the custom fields and taxonomy.

If somebody can please help me out with the query for a mysql or similar you would definitely make my day 🙂

EDIT:
I think wp_update_post is a good way to accomplish this task.

// get all post IDs
$post_ids = get_posts(array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'numberposts'   => -1, // get all posts.
    'fields'     => 'ids', // Only get post IDs
));

// info: custom field 'actor' = 'Tom Hanks'

// Update all posts
foreach ($post_ids as $id) {

  $excerpt =  get_post_meta($id, 'excerpt', true);  // for testing purpose I added a customfield 'excerpt'
    // info: custom field 'actor' = 'Tom Hanks'
  $customfield_value = get_post_meta($id, 'actor', true);
  // change the custom field values to slug format:  'Tom Hanks' > 'tom-hanks'
  $customfield_value = sanitize_title_with_dashes($customfield_value);


  $post = array(
    'ID' => $id,      // select all posts
    'post_excerpt' => $excerpt, // update the excerpt field with the customfield 'excerpt'
    'tax_input'      => array(
      'actor' => array(
        $customfield_value
      )
    )
  );

  wp_update_post( $post );

  // delete the old post meta after we moved it to the taxononmy
  //delete_post_meta($id, 'actor');

}

WordPress Development Asked by Gerald on December 26, 2020

1 Answers

One Answer

You've got the right idea but you are using some of the functions and function arguments incorrectly.

// get all post IDs
$post_ids = get_posts(
  array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'numberposts'   => -1, // get all posts.
    'fields'     => 'ids', // Only get post IDs
  )
);

// info: custom field 'actor' = 'Tom Hanks'
$customfield_value = get_post_meta($post_ids, 'actor', true);
// change the custom field values to slug format:  'Tom Hanks' > 'tom-hanks'
$customfield_value = sanitize_title_with_dashes($customfield_value);

// Update all posts
foreach ($post_ids as $id) {
  $post = array(
    'ID' => $id,      // select all posts - not sure if that works with the array from $post_ids.
    'tax_input'      => array( 
      'actor' => array( 
        $customfield_value
      ) 
    )
  );
  // Update the post into the database
  wp_update_post( $post );

  // delete the old post meta after we moved it to the taxononmy
  delete_post_meta($id, 'actor'); 
}

That is untested. Do not run that on a production database without first testing it on a copy first. Be very, very careful especially with deleting the post meta. That is not reversible.

Answered by s_ha_dum on December 26, 2020

Add your own answers!

Related Questions

List posts based on first letter of posts

2  Asked on September 13, 2020 by shahinul-islam

   

How to redirect a page to another?

1  Asked on September 13, 2020 by heera

     

How to get the meta title of a page configured as blog (loop)

1  Asked on September 8, 2020 by gerard

 

I am trying to match the wpallimport using the Xpath Filter

1  Asked on September 4, 2020 by ankit-prajapati

     

how to list all post that are in the custom taxonomy using $wpdb

0  Asked on September 3, 2020 by kenneth-gervacio

   

How to pass multiple values in a form for tax_query?

0  Asked on August 21, 2020 by irishrunner16

   

How to add post_distinct filter to WP_Comment_Query?

2  Asked on August 17, 2020 by jonathan-gruber

   

How can I combine one field using wpdb and group by?

1  Asked on August 10, 2020 by user81828

   

Custom posts password protect

1  Asked on August 6, 2020 by jason

   

REST API: Display Category names in JSON?

1  Asked on August 3, 2020 by steve

       

Gutenberg withInstanceId. When to use it?

2  Asked on July 25, 2020 by at-least-three-characters

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.