WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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 '11 at 13:11

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.

share|improve this answer

Your Answer


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.