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 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

1 Answer 1

here is how i would do that:

add_action('save_post','update_all_meta');

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 ) 
        return;
    //check for post type
    if ( $post->post_type != "YOUR_CUSTOM_TYPE" ) 
        return;
    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 ) {
        setup_postdata($post); 
        update_post_meta($post->ID,'meta_key',$meta_value);
    }
    $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

 
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.