Replies: 0
I found a solution online but it just wasn’t enough. It’s currently pulling the terms, but I wanted them to be relative to the post that I’m on.
What I mean, and to give more context. I am posting a cf7 form on a job posting. The job posting has a taxonomy for location terms. I am displaying these terms on the page itself as part of the job description.
The dropdown shows ALL terms though, I need it to show only the ones that the post displays, otherwise the user could select a location that’s not applicable.
This is the code:
function dynamic_select_list( $tag ) {
// Only run on select lists
if( 'select' !== $tag['type'] && ('select*' !== $tag['type']) ) {
return $tag;
} else if ( empty( $tag['options'] ) ) {
return $tag;
}
$term_args = array();
// Loop thorugh options to look for our custom options
foreach( $tag['options'] as $option ) {
$matches = explode( ':', $option );
if( ! empty( $matches ) ) {
switch( $matches[0] ) {
case 'taxonomy':
$term_args['taxonomy'] = $matches[1];
break;
case 'parent':
$term_args['parent'] = intval( $matches[1] );
break;
}
}
}
// Ensure we have a term arguments to work with
if( empty( $term_args ) ) {
return $tag;
}
// Merge dynamic arguments with static arguments
$term_args = array_merge( $term_args, array(
'hide_empty' => false,
) );
$terms = get_terms( $term_args );
// Add terms to values
if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {
foreach( $terms as $term ) {
$tag['values'][] = $term->name;
}
}
return $tag;
}
add_filter( 'wpcf7_form_tag', 'dynamic_select_list', 10 );