Limit the Number of Listings

For anyone hoping to limit the amount of listings per day, use this code:

add_filter(
	'hivepress/v1/forms/listing_submit/errors',
	function( $errors, $form ) {
		$listing = $form->get_model();

		if ( $listing && $listing->get_user__id() ) {
			$listing_count = \HivePress\Models\Listing::query()->filter(
				[
					'status__in' => [ 'publish', 'pending', 'draft' ],
					'user'       => $listing->get_user__id(),
					'date_query' => [
						[
							'after'     => '1 day ago',
							'inclusive' => true,
						],
					],
				]
			)->get_count();

			if ( $listing_count >= 10 ) {
				$errors[ ] = 'Only 10 Listings per day are allowed.';
			}
		}

		return $errors;
	},
	1000,
	2
);

Where to edit:

  • change “10” to the amount of listings per day
  • if you would like to limit the amount of listings per week, change ‘after’ => ‘1 day ago’, to ‘after’ => ‘1 week ago’,.
  • if you would like to limit the amount of listings per month, change ‘after’ => ‘1 day ago’, to ‘after’ => ‘1 month ago’,.
  • don’t forget to change $errors if you are changing the limit!
2 Likes

Hi,

Thank you for your solution, it will be useful for our community.

Hi,

This solution is not working !

@ihor mentioned that :
" Regarding the listing query, currently there’s no “between” operator support for dates in our API, but you can always use a fallback for the WP_Query, via $query->set_args($args) you can provide the WP_Query args for dates WP_Query – Class | Developer.WordPress.org ."
Source : Customizing vendor images and query arguments

Using “date_query” seems not to be compatible with Hivepress API.

Please, let me know if I am wrong…

i think UR correct, i am using buildin WP Query

Yes, please try using the same snippet but move the date_query to the ->set_args() after the ->filter(), it should be ok then.

		$listing_count = \HivePress\Models\Listing::query()->filter(
			[
				'status__in' => [ 'publish', 'pending', 'draft' ],

				//Others filters here
			]
		)->set_args(
			['date_query' => [
				[
					'after'     => '1 day ago',
					'inclusive' => true,
				],
			],
			]
		)
		->get_count();

Yes, this solution will work indeed.
Many thanks @ihor

2 Likes

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