Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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');

}
share|improve this question

1 Answer 1

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.

share|improve this answer
    
thanks for improving the code. That gets me a few steps forward. I updated the source above: I moved the customfields into the foreach loop. For testing purpose I created an excerpt custom field. Updating the excerpt field works great but the taxonomy is not changed. The WP-codex lists the 'tax_input' only in 'wp_insert_post' function. Possibly it's not working with wp_update_post? I`m clueless –  Gerald Sep 4 '13 at 21:46
    
my last hope was this to update the posts with the actor post 'tax_input' => array( 'post_type' => array('actor' => 'tom-hanks'), ) I can change the taxonomies but since tom-hanks is a post in the CPT it does not work –  Gerald Sep 6 '13 at 16:04
    
I don't understand y our last comment –  s_ha_dum Sep 6 '13 at 16:15
    
I can change the taxonomies via wp_set_object_terms. But 'tom hanks' is not a taxonomy. It`s a post in the custom post type (actor). with wp_update_post I can only change the post infos like title and tags but I need to update the relationship between the post and the cpt-post. edit: the tax_input we used in the code above only accepts taxonomies but no post_type. –  Gerald Sep 6 '13 at 16:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.