Different number of uploaded images in ads for different categories

Hello! Please help me write a snippet that allows to upload a different number of images in the listing in different categories. Depending on the category id. I tried to modify this code in different ways, but without success:

add_filter(
	'hivepress/v1/models/listing',
	function( $model ) {
		$model['fields']['images']['max_files'] = 2;

		return $model;
	},
	100
);

Hi,

Sorry, there’s no simple code snippet for this, it would require a custom implementation. If customizations beyond the available features are required for your site, please consider hiring someone for custom work https://fvrr.co/32e7LvY

I’m trying to write a snippet, but not everything works, there’s an error somewhere. If it’s not difficult, point to it.

add_filter(
	'hivepress/v1/models/listing',
    function( $model ) {

    $category_ids = [ 42, 44, 45 ]; // your selection of the necessary categories
    $max_photos = 1; // the default number of photos, if the category is not specified

    // Getting the current category
    if ( is_singular( 'hp_listing' ) ) {
        global $post;
        $category_terms = get_the_terms( $post->ID, 'hp_listing_category' );
        if ( ! empty( $category_terms ) ) {
            $category_ids = wp_list_pluck( $category_terms, 'term_id' );
        }
    } elseif ( is_tax( 'hp_listing_category' ) ) {
        $category_ids = array( get_queried_object()->term_id );
    }

    // Setting the maximum number of photos, depending on the category
    if ( in_array( 42, $category_ids ) ) {
        $max_photos = 3;
    } elseif ( in_array( 44, $category_ids ) ) {
        $max_photos = 3;
    } elseif ( in_array( 45, $category_ids ) ) {
        $max_photos = 4;
    }

    $model['fields']['images']['max_files'] = $max_photos;

    return $model;

}, 100 );

Please try this PHP code snippet. Please change 100 on the category id where you want to add the restriction, and please change 200 on the max images for listings in this category. In this way, you can add max image restriction for other categories. Please note that it can require further customization. If you are not familiar with the code customization, then please consider hiring someone for custom work https://fvrr.co/32e7LvY

add_filter(
	'hivepress/v1/models/listing/errors',
	function($errors, $model){
		if(!empty($errors)){
			return $errors;
		}
		
		$categories_id = $model->get_categories__id();
		$images_count = count((array)$model->get_images__id());
		
		if(in_array(100, $categories_id) && $images_count < 200){
			$errors[] = 'Custom error';
		}else{
			$errors[] = 'Second custom error';
		}
		
		return $errors;
	},
	1000,
	2
);

add_filter(
	'hivepress/v1/models/listing',
	function($model){
		$model['fields']['images']['max_files'] = 1000;
		return $model;
	},
	1000,
	2
);
1 Like

Thank you very much for the answer! But when I adding a new ad, I select the category I need (I made edits to the snippet), the restriction condition does not work anyway, but only the condition works:

add_filter(
	'hivepress/v1/models/listing',
	function($model){
		$model['fields']['images']['max_files'] = 1000;
		return $model;
	},
	1000,
	2
);

Please try to make changes only in this part of the code snippet. For example, there is a restriction to listing those in the category with ID 26. If listings in this category have less than 10 images, then a Custom error appears. So please check this example and try to make changes by your requirements

add_filter(
	'hivepress/v1/models/listing/errors',
	function($errors, $model){
		if(!empty($errors)){
			return $errors;
		}
		
		$categories_id = $model->get_categories__id();
		$images_count = count((array)$model->get_images__id());
		
		if(in_array(26, $categories_id) && $images_count < 10){
			$errors[] = 'Custom error';
		}
		
		return $errors;
	},
	1000,
	2
);

This part of code snippet just have 1000 number to avoid default maximum files errors you can change it to any number which is more than number in the previous part of the code snippet

add_filter(
	'hivepress/v1/models/listing',
	function($model){
		$model['fields']['images']['max_files'] = 1000;
		return $model;
	},
	1000,
	2
);
1 Like

I need the snippet to trigger on the new ad submission page when I select a category, for example, real estate can upload 3 images, services -2 images, etc. These examples didn’t work… Weeping

Please try this PHP code snippet. Please change 100 on the category id where you want to add the restriction, and please change 200 on the max images for listings in this category. In this way, you can add max image restriction for other categories. Please note that it can require further customization. If you are not familiar with the code customization, then please consider hiring someone for custom work https://fvrr.co/32e7LvY

add_filter(
	'hivepress/v1/models/listing/errors',
	function($errors, $model){
		if(!empty($errors) || !$model->get_id()){
			return $errors;
		}
		
		$categories_id = (array)$model->get_categories__id();
		$images_count = count((array)$model->get_images__id());
		
		if(in_array(100, $categories_id) && $images_count < 200){
			$errors[] = 'Custom error';
		}else{
			$errors[] = 'Second custom error';
		}
		
		return $errors;
	},
	1000,
	2
);

add_filter(
	'hivepress/v1/models/listing',
	function($model){
		$model['fields']['images']['max_files'] = 1000;
		return $model;
	},
	1000,
	2
);

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.