WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] delete_post_meta problem for multiple meta entries using the same metakey (3 posts)

  1. jakemc
    Member
    Posted 3 years ago #

    Hi. I want to be able to assign WP users to individual posts (in this case a custom content type called 'member organisation'). I use a custom $wpdb query to list all users and can associate them to the MO by creating a new postmeta entry with key 'mo-user' and value of the user's ID.

    However, I can't delete them. Here's the code I'm using:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    Any help appreciated. Thanks.
    Jake

  2. jakemc
    Member
    Posted 3 years ago #

    Here's the pastebin URL:
    http://wordpress.pastebin.com/3tRCzjuQ

    Thanks in advance.

  3. jakemc
    Member
    Posted 3 years ago #

    After much digging I found this solution:
    I got the id of the postmeta entry :

    $assoc_users_sql = "SELECT m.meta_id, u.user_login
    					FROM $wpdb->postmeta m, $wpdb->users u
    					WHERE m.post_id = $post->ID
    					AND m.meta_key = 'mo-user'
    					AND m.meta_value = u.ID
    					ORDER BY u.user_login
    					";

    For each listed user I used the id of the postmeta entry instead of the meta_value:
    <input type="submit" name="deleteuser[<?php echo $a_user->meta_id; ?>]" class="deleteuser" value="Delete" />
    and then delete_meta to remove the record:

    if ( isset($_POST['deleteuser']) && $_POST['deleteuser'] ) {
    		foreach ( $_POST['deleteuser'] as $key => $value ) {
    			delete_meta($key );
    		}
    	}

Topic Closed

This topic has been closed to new replies.

About this Topic