I have two custom post types (cpt_business and cpt_deal) on my website, each with its own set of custom fields managed by Advanced Custom Fields (ACF):
cpt_business: Managed by the “Business Information” ACF group.
cpt_deal: Managed by the “Deals Information” ACF group.
In the “Deals Information” ACF group, I have added a relationship field named related_business which links to posts of type cpt_business. This allows me to associate each cpt_deal post with a cpt_business post.
Objective
I aim to create a front-end form using tagDiv Composer that allows users to create or edit cpt_deal posts. This form should include the related_business relationship field so that users can select the associated cpt_business post when submitting or editing a deal.
Requirements
Front-End Form Integration: I need guidance on how to configure the front-end form in tagDiv Composer to include and handle the related_business ACF relationship field for cpt_deal posts.
Data Retrieval and Display: Additionally, I have a shortcode that needs to retrieve and display the contact information (country code and WhatsApp number) from the associated cpt_business post on the front-end, based on the related_business field.
Example Shortcode
Here is the shortcode I am using to display the WhatsApp link:
function smart_whatsapp_link_with_acf_shortcode($atts) {
$attributes = shortcode_atts(array(‘urlencode’ => ‘true’), $atts);
if (is_singular(array(‘cpt_business’, ‘cpt_deal’))) {
$page_url = get_permalink();
if (!validate_url($page_url) || !is_safe_protocol($page_url)) return ‘Invalid URL’;
$country_code = ”;
$phone_number = ”;
if (get_post_type() == ‘cpt_deal’) {
$related_business = get_field(‘related_business’);
if ($related_business) {
$country_code = get_field(‘country_code’, $related_business->ID);
$phone_number = get_field(‘whatsapp_number’, $related_business->ID);
}
} else {
$country_code = get_field(‘country_code’);
$phone_number = get_field(‘whatsapp_number’);
}
if (!$country_code || !$phone_number) return ‘Missing WhatsApp contact information’;
$phone_number = preg_replace(‘/[^0-9]/’, ”, $phone_number);
$message = “Hello! I’m interested in this deal listed on Shockvibes:\n\n”;
$encoded_message = ($attributes[‘urlencode’] === ‘true’) ? urlencode($message) . rawurlencode($page_url) : $message . $page_url;
$whatsapp_link = ‘https://wa.me/’ . $country_code . $phone_number . ‘?text=’ . $encoded_message;
return $whatsapp_link;
} else {
return ‘No WhatsApp link available’;
}
}
add_shortcode(‘smart_whatsapp_link_with_acf’, ‘smart_whatsapp_link_with_acf_shortcode’);