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.

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!

share|improve this question
    
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! –  userabuser Jun 22 '12 at 16:20
    
I have about 200 posts something, want to execute command in phpMyadmin. –  Jack Jun 23 '12 at 0:26
add comment

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 ) )
                continue;
           $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.

share|improve this answer
add comment

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.