Hi Tessa ?? ,
Thank you so much for your response – I really appreciate it. But something strange happens and it shows another ‘ID number’ than the custom taxonomy term ID I want or the one added by WordPress.
I tested with the code you gave just replaced the taxonomy’s slug ‘jobs-id‘ here:
// For simplicity's sake, define custom taxonomy here
$taxonomy = 'jobs-id'; // I'm assuming this is your custom taxonomy's slug
with the one based on my setup in CPT UI – ‘id_jobs‘. taxonomy slug
Here you can see screenshot of the mail template: form template
… and what the test page with the contact form looks like: screenshot of the page See the correct Dynamic ID JOB: 20029 and the wrong one “32”, which I have no idea where it generated from. Any ideas what’s wrong and where it’s coming from?
Thank you. ??
p.s. I also tested a code that generated a chat GPT based on the one you give and it says: “The following function retrieves the term ID of your custom taxonomy id_jobs
and returns it for use in Contact Form 7. Key Changes:
get_the_ID()
replaces wpcf7dtx_get_post_id(false)
to retrieve the post ID, which is more reliable unless you specifically need the helper function.
- update the dynamic text field in your Contact Form 7 form to use the new shortcode:
[dynamictext job_id "CF7_get_post_var key='akewizjg_job_id'"]
function akewizjg_job_id() {
// Get the current post ID using WordPress function (in case the other one isn't available)
$post_id = get_the_ID();
if (!$post_id) return ''; // Bail on failure
// Define the custom taxonomy slug here
$taxonomy = 'id_jobs'; // Ensure this is the correct slug from CPT UI (replace 'id_jobs' with the actual slug if different)
// Get the terms associated with the current post for the custom taxonomy
$terms = get_the_terms($post_id, $taxonomy);
// Bail if no terms are found or if the result is not an array
if (!is_array($terms) || !count($terms)) return '';
// Return the term ID of the first term
return $terms[0]->term_id; // Use term_id to return the custom taxonomy term ID
}
add_shortcode('akewizjg_job_id', 'akewizjg_job_id');
I had to try…. however, nothing changed and the field remained empty. ??
-
This reply was modified 1 month ago by akewizjg.