I have a form where i list a related custom post type by ID of the parent custom post type on a checkbox list like the picture.
so when i check an option to 4 options i want to have a list of the terms that have the custom posts i checked on the dropdown list.
the form is constructed with elementor pro but to have the list of posts and the taxonomy terms i do it with code (i do my function and i include it with shortcode on functions.php).
/*
* Display list of relationship field
*
*/
function list_formation_caces(){
echo "<div class='golistgo'>";
$args = array(
'post_type' => 'categories',
'posts_per_page' => 20,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$formations = get_posts(array(
'post_type' => 'formations',
'post__in' => array(16061),
'meta_query' => array(
array(
'key' => 'categories', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exactly "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
));
if($formations){
foreach ($formations as $categorie){
$image = get_field('image_form');
if($image){
// Image variables.
$url = $image['url'];
$title = $image['title'];
$alt = $image['alt'];
$caption = $image['caption'];
// Thumbnail size attributes.
$size = 'thumbnail';
$thumb = $image['sizes'][ $size ];
$width = $image['sizes'][ $size . '-width' ];
$height = $image['sizes'][ $size . '-height' ];
}
echo "<input type=\"checkbox\" class=\"btn-check\" name=\"Description\" value =\"". get_the_title() .
"\" id=\"". get_the_ID() ."\">";
echo "<label class=\"options-form\" for='". get_the_ID() ."'>";
echo "<div class=\"image-cat\">";
echo get_the_post_thumbnail();
echo"</div>";
echo "<div class=\"body-cat\">";
echo "<div class=\"title-formation\">" ;
echo "</div>";
echo "<div class=\"title-cat\">" ;
echo the_title() . ":" . " <span class=\"describe-cat\">" . get_field('type_machine') . "</span>";
echo "</div>";
echo "</div>";
echo "</label>";
}
}
endwhile;
wp_reset_postdata();
echo "</div>";
}
function shortcode_list_categ_caces(){
return (list_formation_caces()) ;
}
add_shortcode('list_formation_caces', 'shortcode_list_categ_caces');
function shortcode_total_count_checked(){
return ("<div class='count-checkboxes-wrapper'>" . "<h2 class='title-count'>" . "Vous avez choisis" .
"<span id='count-checked-checkboxes'> " . " 0" ."</span> catégorie(s)."." </h2>" .
"</div>");
}
add_shortcode('count_checked', 'shortcode_total_count_checked');
function list_sessions_caces()
{
$terms = get_terms( array(
'taxonomy' => 'sessions',
'hide_empty' => false,
) );
echo'<label for="form-field-centre" class="elementor-field-label">Où se déroulera la formation ?</label>';
echo '<div class="elementor-field elementor-select-wrapper ">';
echo '<select name="CENTRE" id="form-field-field_centre" class="elementor-field-textual elementor-size-sm">';
echo '<option value="">Choisir une session de formation </option> ';
foreach ($terms as $term) {
echo '<option value="' . $term->name . '">' . $term->name . '</option>';
}
echo '</select>';
echo'</div>';
}
function shortcode_list_sessions(){
return (list_sessions_caces()) ;
}
add_shortcode('list_sessions_caces', 'shortcode_list_sessions');
Any help, please!
I have a form where i list a related custom post type by ID of the parent custom post type on a checkbox list like the picture.
so when i check an option to 4 options i want to have a list of the terms that have the custom posts i checked on the dropdown list.
the form is constructed with elementor pro but to have the list of posts and the taxonomy terms i do it with code (i do my function and i include it with shortcode on functions.php).
/*
* Display list of relationship field
*
*/
function list_formation_caces(){
echo "<div class='golistgo'>";
$args = array(
'post_type' => 'categories',
'posts_per_page' => 20,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$formations = get_posts(array(
'post_type' => 'formations',
'post__in' => array(16061),
'meta_query' => array(
array(
'key' => 'categories', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exactly "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
));
if($formations){
foreach ($formations as $categorie){
$image = get_field('image_form');
if($image){
// Image variables.
$url = $image['url'];
$title = $image['title'];
$alt = $image['alt'];
$caption = $image['caption'];
// Thumbnail size attributes.
$size = 'thumbnail';
$thumb = $image['sizes'][ $size ];
$width = $image['sizes'][ $size . '-width' ];
$height = $image['sizes'][ $size . '-height' ];
}
echo "<input type=\"checkbox\" class=\"btn-check\" name=\"Description\" value =\"". get_the_title() .
"\" id=\"". get_the_ID() ."\">";
echo "<label class=\"options-form\" for='". get_the_ID() ."'>";
echo "<div class=\"image-cat\">";
echo get_the_post_thumbnail();
echo"</div>";
echo "<div class=\"body-cat\">";
echo "<div class=\"title-formation\">" ;
echo "</div>";
echo "<div class=\"title-cat\">" ;
echo the_title() . ":" . " <span class=\"describe-cat\">" . get_field('type_machine') . "</span>";
echo "</div>";
echo "</div>";
echo "</label>";
}
}
endwhile;
wp_reset_postdata();
echo "</div>";
}
function shortcode_list_categ_caces(){
return (list_formation_caces()) ;
}
add_shortcode('list_formation_caces', 'shortcode_list_categ_caces');
function shortcode_total_count_checked(){
return ("<div class='count-checkboxes-wrapper'>" . "<h2 class='title-count'>" . "Vous avez choisis" .
"<span id='count-checked-checkboxes'> " . " 0" ."</span> catégorie(s)."." </h2>" .
"</div>");
}
add_shortcode('count_checked', 'shortcode_total_count_checked');
function list_sessions_caces()
{
$terms = get_terms( array(
'taxonomy' => 'sessions',
'hide_empty' => false,
) );
echo'<label for="form-field-centre" class="elementor-field-label">Où se déroulera la formation ?</label>';
echo '<div class="elementor-field elementor-select-wrapper ">';
echo '<select name="CENTRE" id="form-field-field_centre" class="elementor-field-textual elementor-size-sm">';
echo '<option value="">Choisir une session de formation </option> ';
foreach ($terms as $term) {
echo '<option value="' . $term->name . '">' . $term->name . '</option>';
}
echo '</select>';
echo'</div>';
}
function shortcode_list_sessions(){
return (list_sessions_caces()) ;
}
add_shortcode('list_sessions_caces', 'shortcode_list_sessions');
Any help, please!
You can go two ways:
cpt-134
and then on click the CPT, set the dropdown data to the array named cpt-[id-of-cpt]
. One way to get this data into your javascript is, if you e.g. have a general.js
:wp_register_script('general', get_template_directory_uri() . '/js/general.js');
wp_localize_script('general', 'myVars', array(
'site_url' => site_url(),
'dropdownData' => get_CPT_Dropdown_Data(),
));
wp_enqueue_script('general');
You would ofcourse have to write that function get_CPT_Dropdown_Data()
that returns the arrays with the taxonomy data, based on the CPT id's.
Option 2 is a more future-proof method (if things get bigger) and will not bloat your code with data that might not be needed. But it will add round trips to the server, so you might want to incorporate feedback for your users for when the loading starts.