custom field - limit amount of photos uploaded per cpt post

admin2025-06-05  3

On my site I have a front end form for users to create a post and upload images. It works fine however I cant seem to figure out how to limit the number of images per post.

The image attachments get saved to a custom field, so is there a way to limit the amount of values allowed to the custom field?

I also came across this post but was unable to get it to work.

current code in use for uploading files --

// THE PHOTO UPLOAD HERE
if(isset($_POST["savepics2"])) {

$attachments = get_children( array( 'post_parent' => $v_Id ) );
$count = count( $attachments );
if ($count == 25) {
echo 'limit reached';
} else {


if (!empty($_FILES['vidPix']['tmp_name'][0])) {
                    $i = 1;
                    $files = $_FILES['vidPix'];
                    foreach ($files['name'] as $key => $value) {
                        if ($files['name'][$key]) {
                            $file = array(
                                'name' => $files['name'][$key],
                                'type' => $files['type'][$key],
                                'tmp_name' => $files['tmp_name'][$key],
                                'error' => $files['error'][$key],
                                'size' => $files['size'][$key]
                            );
                            $_FILES = array("sight" . $i => $file);
add_filter( 'upload_dir', 'wpse_141088_upload_dir' );
add_filter('intermediate_image_sizes_advanced', 'no_image_resizing');

                        $mfile =  wp_handle_upload($files, $upload_overrides );             

                            $newvidPix = sight("sight" . $i, $v_Id);
remove_filter( 'upload_dir', 'wpse_141088_upload_dir' );
remove_filter('intermediate_image_sizes_advanced', 'no_image_resizing');
// Convert the image to PNG and delete the old image.
attachment_to_png( $newvidPix );

                            if ($i == 1) {
                                update_post_meta($v_Id, '_thumbnail_id', $newvidPix);
                            }
                                add_post_meta($v_Id, 'vid_pix', $newvidPix, false);
                        }
                        $i++;
                    }
                }
        }
}

Any ideas? thanks.

On my site I have a front end form for users to create a post and upload images. It works fine however I cant seem to figure out how to limit the number of images per post.

The image attachments get saved to a custom field, so is there a way to limit the amount of values allowed to the custom field?

I also came across this post but was unable to get it to work.

current code in use for uploading files --

// THE PHOTO UPLOAD HERE
if(isset($_POST["savepics2"])) {

$attachments = get_children( array( 'post_parent' => $v_Id ) );
$count = count( $attachments );
if ($count == 25) {
echo 'limit reached';
} else {


if (!empty($_FILES['vidPix']['tmp_name'][0])) {
                    $i = 1;
                    $files = $_FILES['vidPix'];
                    foreach ($files['name'] as $key => $value) {
                        if ($files['name'][$key]) {
                            $file = array(
                                'name' => $files['name'][$key],
                                'type' => $files['type'][$key],
                                'tmp_name' => $files['tmp_name'][$key],
                                'error' => $files['error'][$key],
                                'size' => $files['size'][$key]
                            );
                            $_FILES = array("sight" . $i => $file);
add_filter( 'upload_dir', 'wpse_141088_upload_dir' );
add_filter('intermediate_image_sizes_advanced', 'no_image_resizing');

                        $mfile =  wp_handle_upload($files, $upload_overrides );             

                            $newvidPix = sight("sight" . $i, $v_Id);
remove_filter( 'upload_dir', 'wpse_141088_upload_dir' );
remove_filter('intermediate_image_sizes_advanced', 'no_image_resizing');
// Convert the image to PNG and delete the old image.
attachment_to_png( $newvidPix );

                            if ($i == 1) {
                                update_post_meta($v_Id, '_thumbnail_id', $newvidPix);
                            }
                                add_post_meta($v_Id, 'vid_pix', $newvidPix, false);
                        }
                        $i++;
                    }
                }
        }
}

Any ideas? thanks.

Share Improve this question edited Dec 3, 2018 at 18:45 730wavy asked Nov 13, 2018 at 15:03 730wavy730wavy 1931 gold badge13 silver badges45 bronze badges 2
  • Can you update the question with your code written so far? – middlelady Commented Nov 13, 2018 at 17:08
  • sorry about the delay. I've updated the question with code. – 730wavy Commented Dec 3, 2018 at 18:46
Add a comment  | 

1 Answer 1

Reset to default 1

You can do it like so — refer to the // {comment}:

if (isset($_POST["savepics2"])) {
    // Set max number of files allowed.
    $max_files = 25;

    $attachments = get_children( array( 'post_parent' => $v_Id ) );
    $count = count( $attachments );

    // Check if limit already reached.
    if ($count >= $max_files) {
        echo 'limit reached';
    // If not, then upload the files.
    } else {
        if (!empty($_FILES['vidPix']['tmp_name'][0])) {
            $i = 1;
            $files = $_FILES['vidPix'];

            foreach ($files['name'] as $key => $value) {
                // Check if limit already reached.
                if ( $count >= $max_files ) {
                    echo 'limit reached';
                    break;
                }

                // If not, then upload next file.
                if ($files['name'][$key]) {
                    ...your code here...
                }

                $i++;
                $count++; // increment the count
            }
        }
    }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749101660a316373.html

最新回复(0)