Skip to main content
added 7 characters in body
Source Link
Bainternet
  • 67.7k
  • 8
  • 131
  • 188

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.

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' => $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.

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.

Source Link
Bainternet
  • 67.7k
  • 8
  • 131
  • 188

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' => $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.