I have a custom post type with some custom fields in it.

Now what i want to do is when i save a new OR edit an existing post in that post type, is to change custom fields from all the other posts of that custom post type.

I'm familiar with the update_post_meta, but i can't figure out on how to get the post ID's of all the other posts to update them all.

  • Allready figured it out! Just by using the normal WP_Query function! Here's my snippet: // here i set a var for the post id that i actualy edited so that later on i can make sure the edited post is not being changed again $postedid = $post->ID; $getmyposts = new WP_Query('post_type=mycustomposttype&posts_per_page=-1'); while ( $getmyposts->have_posts() ) : $getmyposts->the_post(); //check if it is the post i just edited, otherwise change it if ( $post->ID == $postedid ) { } else { update_post_meta($post->ID, "metatermname", yourvalue); } endwhile;
    – Alex
    Jun 7, 2011 at 13:11

1 Answer 1


here is how i would do that:


function update_all_meta($post_id){
    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
    //check for post type
    if ( $post->post_type != "YOUR_CUSTOM_TYPE" ) 
    global $post;
    $tmp_post = $post;
    $args = array( 
        'posts_per_page' => -1,
        'post_type' => 'YOUR_CUSTOM_TYPE',
        'post__not_in' => (array)$post->ID //skip current post

    $myposts = get_posts( $args );
    //loop over all custom posts and update the meta
    foreach( $myposts as $post ) {
    $post = $tmp_post;

but if the stored data is the same maybe use options api so you only update it one and you can use it for all posts.

Your Answer

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

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