0

I have a custom post type called 'programmes' that details seminar information. Using a form visitors to the site can signup to a specific 'programme', once they have completed the form a new user is created with the following user meta:

  • programme_id - the post ID of the 'programme' registered on
  • programme_name - the title of the 'programme' registered on
  • programme_end_date - the date the 'programme' ends

The 'programme' dates are often changed (due to venue availability etc) after users have already registered, as a result I need to update the programme_end_date user meta field when these changes happen.

I know it's going to be some combination of the save_post action along with update_user_meta and presumably a loop to check for all the users that have the specific updated 'programme' ID in the programme_id user meta field, but I can get my head round exactly how to do it. Any pointers would be excellent.

1

You could query all the authors and loop through to update their dates. The below query pulls all users who have a programme_id of the current post_id and allocates an array of their IDs. You can then loop through to update whatever is necessary:

/** Save Custom Metaboxes **/
function save_custom_meta_boxes( $post_id, $post ) {

    /** 
     * Your Testing conditions should go here to ensure
     * That we're in the correct place and actually need to
     * run this query. I suggest checking against post type 
     */

    $user_query = new WP_User_Query( array(
        'meta_key'  => 'programme_id',
        'meta_value'=> $post_id,
        'fields'    => 'ID',
    ) );

    if( ! empty( $user_query->results ) ) {
        foreach( $user_query->results as $user_id ) {
            update_user_meta( $user_id, 'programme_end_date', $_POST['some_data_here'] );
        }
    }
}
add_action( 'save_post', 'save_custom_meta_boxes', 10, 2 );

I kept it as simple as possible and if you have a ton of users this may not be optimal.

  • I think you understood the question better than I did. :) – Christine Cooper Mar 16 '16 at 17:36
  • Sorry only just got back round to this. Your answer looks excellent and I can follow the logic, however for some reason $user_id is passing an array of values(!) as opposed to the actual ID, any idea? – charliek Mar 21 '16 at 14:02
  • @charliek Whoops! Fixed that. WP_Query puts it's results into a posts[] array. Usually it's an array of Post objects ( $post->ID, $post->post_date, etc ) but since we're only returning 1 field (ID) it's returning an array in integers. Anyway, to access this array we need to loop through $user_query->posts instead. If this solves you issue please ensure th click the checkmark so future readers know it's solved!. – Howdy_McGee Mar 21 '16 at 14:07
  • This throws the follow errors - Invalid argument supplied for foreach() in /.../functions.php on line 420 and Warning: Cannot modify header information - headers already sent by (output started at /.../functions.php:420) in /.../wp-includes/pluggable.php on line 1196 – charliek Mar 21 '16 at 17:26
  • Have solved it. We we missing` $user_query->get_results()` – charliek Mar 21 '16 at 17:35
0

I am a bit unsure what your aim is but I can definitely get you on your feet.

So you want to hook into save_post, confirm which post_status your are targeting, and then update user/post meta as you wish.

I've written this for you, see the comments for clarification and adjust it as you please:

add_action('save_post', 'wp_220873', 10, 2);

function wp_220873($id, $p) {

    // confirm if post is being published (or whatever post status you're targetting)
    if (
        (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        || (defined('DOING_AJAX') && DOING_AJAX)
        || ($p->post_status !== 'publish')
    ) {
        return;
    }

    // update post meta as you wish
    update_post_meta( $p->ID, 'meta_key', $value );

    // update user meta as you wish
    update_user_meta( $p->post_author, 'meta_key', $value );
}
0

try this in the save_post hook

// search users which have this programme id

$args = array(
    "meta_query" => [
        [
            "key" => "programme_id",
            "value" => $programme_id,
        ]
    ],
);

$wp_user_search = new \WP_User_Query($args);
$items = $wp_user_search->get_results();


foreach ($items as $user) {

    // update end date

    update_user_meta($user->ID, "programme_end_date", $programme_end_date);


}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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