Question/issue (workaround is posted below):
How one prevents submission of a “Add listing” form in case certain field (in my case custom fields of a type Text, with a Field Name “messengera_address”, “messengerb_address”) contains input which which already exist in some listing (meaning a listing with same “messengera_address” for example) ?
Solution/feature request:
When editing attribute of a listing WP-Admin > Listings > Attributes > attribute name, it can be handy to have a checkbox labeled e.g. “Check and avoid duplicate submissions” (? → It will be impossible to submit more than one listing with same value of this field)
Solution/workaround:
Use WP plugin “Code snippets” (or similar) and insert in it a PHP code:
add_filter(
'hivepress/v1/forms/listing_update/errors',
function( $errors, $form ) {
$current_listing_id = $form->get_model()->get_id();
// Check messengera_address
$messengera_address = $form->get_value( 'messengera_address' );
if ( $messengera_address ) {
$listing_id = \HivePress\Models\Listing::query()->filter(
[
'status__in' => [ 'draft', 'pending', 'publish' ],
]
)->set_args(
[
'meta_key' => 'hp_messengera_address',
'meta_value' => $messengera_address,
]
)->get_first_id();
if ( $listing_id && $listing_id !== $current_listing_id ) {
$errors['messengera_address'] = 'This messengera_address address is already listed.';
}
}
// Check messengerb_address
$messengerb_address = $form->get_value( 'messengerb_address' );
if ( $messengerb_address ) {
$listing_id = \HivePress\Models\Listing::query()->filter(
[
'status__in' => [ 'draft', 'pending', 'publish' ],
]
)->set_args(
[
'meta_key' => 'hp_messengerb_address',
'meta_value' => $messengerb_address,
]
)->get_first_id();
if ( $listing_id && $listing_id !== $current_listing_id ) {
$errors['messengerb_address'] = 'This messengerb_address address is already listed.';
}
}
return $errors;
},
10,
2
);
replace messengera_address and messengerb_address with your own “Field Name“ found at WP-Admin > Listings > Attributes > attribute name.