Support

Account

Forum Replies Created

  • Has this issue been resolved yet?

    The Gallery field is useless on mobile devices…

  • I’ve restricted my users with a custom role, granting the ability to manage_photos (their own ones) or manage_others_photos:

    add_filter( 'ajax_query_attachments_args', 'show_only_current_user_attachments', 10 );
    function show_only_current_user_attachments( $query ) {
        $user_id = get_current_user_id();
        if ( $user_id && !current_user_can('activate_plugins') && !current_user_can('edit_others_posts') && !current_user_can('manage_others_photos') ) {
            $query['author'] = $user_id;
    		$query['post_parent'] = 0;
        } elseif ($user_id && !current_user_can('activate_plugins')) {
    		$query['post_parent'] = 0;
    	}
        return $query;
    } 

    I also didn’t want to display images that were already attached to other posts, so I’ve filtered those out too in the version above.

    If you want a basic version without my additions:

    add_filter( 'ajax_query_attachments_args', 'show_only_current_user_attachments', 10 );
    function show_only_current_user_attachments( $query ) {
        $user_id = get_current_user_id();
        if ( $user_id && !current_user_can('activate_plugins') && !current_user_can('edit_others_posts') ) {
            $query['author'] = $user_id;
    	}
        return $query;
    } 
  • I managed to work it out, so am posting it here in case anyone else comes looking for it 🙂

    Points to note:
    – Thinking that it may help someone else, I’ve left a few debugging lines in the below code
    – I am also setting the first entry in the gallery array as the Featured Image / Thumbnail because it isn’t an option I am making available to the users of my site.

    add_action('acf/save_post', 'manage_portfolio_media', 1);
    function manage_portfolio_media($post_id) {
    	
        if ( get_post_type($post_id) == 'ocean_portfolio' ){
    	
            $acf = false;
            if( isset($_POST['acf']) ) {
                $acf = $_POST['acf'];
            }
    
            $field_key = 'field_5e92f3aacc8a6';
            $field_name = 'attachments';
            
            if (isset($acf[$field_key])) {
                $previous_images = get_field($field_name, $post_id);
                $new_images = $acf[$field_key];
    
    			// create a lookup of ids for images in the post
                $new_lookup = array();
                if (!empty($new_images)) {
                    foreach($new_images as $imageid) {
                        array_push($new_lookup, $imageid);
                    }
                }
    
                // create a lookup of images that were removed
                $removed_lookup = array();
                if (!empty($previous_images)) {
                    foreach($previous_images as $image) {
    					error_log('Previous Array = ' . $image['ID']);
                        if (!in_array($image['ID'], $new_lookup)) {
                            array_push($removed_lookup, $image['ID']);
                        }
                    }
                }
    
                if (!empty($removed_lookup)) {
                    foreach($removed_lookup as $imageid) {
                        // unattach removed ones
    					error_log('Removed Item = ' . $imageid);
    					wp_update_post( array( 'ID' => $imageid, 'post_parent' => 0 ) );
                    }
                }
    
                if (!empty($new_lookup)) {
    				set_post_thumbnail( $post_id, $new_lookup['0'] );
    				error_log('New Thumbnail = ' . $new_lookup['0']);
                    foreach($new_lookup as $imageid) {
                        // attach added ones
    					error_log('New Array = ' . $imageid);
    					wp_update_post( array( 'ID' => $imageid, 'post_parent' => $post_id ) );
                    }
                }
            }
        }
    }
  • That code works well for items being added, but doesn’t cater for when they are removed.

    Is there a before and after version of the gallery array available, which I could use to see what was removed and then set the post_parent for those IDs to 0?

  • Apologies for replying to a really old post, but I have the same issue when media is added/removed from the ACF Gallery and the above code no longer works.

    Can anyone point me in the direction of what I need to update?

Viewing 6 posts - 1 through 6 (of 6 total)