I am looking for help on how to pull post name from custom field value

for example

in wp_posts post id = 200, and there are corresponding meta key and values with post_id= 200 in wp_postmeta

I want to update wp_posts set post_name= ...... a value of a specific meta key in wp_postmeta. Please help. Thanks!

  • In what context are you trying to do this? Please provide code example, otherwise I'd say why don't you use the "Title" input box on the post edit screen to set your title. Providing context to the situation you are trying to achieve the above will gain you much more help!
    – Adam
    Commented Jun 22, 2012 at 16:20
  • I have about 200 posts something, want to execute command in phpMyadmin.
    – Jack
    Commented Jun 23, 2012 at 0:26

1 Answer 1


Here is a function that loops through all your posts and checks if the post_name is equal to your post_meta_value. If not it updates the post_name to whatever the post_meta value is.

function wpse_get_update_posts() {
     $posts = get_posts( array( 'posts_per_page' => -1 ) );

         foreach ( $posts as $post ) {
            if ( $post->post_name == get_post_meta( $post->ID, '_your_meta_key', true ) )
           $my_post = array();
           $my_post['ID'] = (int) $post->ID;
           $my_post['post_name'] = get_post_meta( $post->ID, '_your_meta_key', true);
           wp_update_post( $my_post );

You probably only want to run this function one time so add it to a plugin activation hook or call in a place that you can load WordPress once then delete it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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