Get selected options of the checkboxes attribute

My listing has a custom attribute that uses the fieldtype “checkboxes”. I am trying to get the values that the user selected for those checkboxes so that I can display them in the template.
The field name is event_type. Users can selected multiple values, for example (concert, party, workshop) and I want to display them next to the category name.

<?php
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
if ( $listing->get_categories__id() ) :
	?>
	<div class="hp-listing__categories hp-listing__category">
		<?php foreach ( $listing->get_categories() as $category ) : ?>
			<a href="<?php echo esc_url( hivepress()->router->get_url( 'listing_category_view_page', [ 'listing_category_id' => $category->get_id() ] ) ); ?>"><?php echo esc_html( $category->get_name() ); ?></a>
		    <?php if ($category->get_id()==42 ) : ?>
		        <?php foreach ($listing -> get_eventtype() as $eventType ) : ?>
		         <a href="url_here"><?php echo esc_html( $eventType ); ?></a>
		        <?php endforeach; ?>
	    	<?php endif; ?>
		<?php endforeach; ?>
	</div>
	<?php
endif;

The line I am having issue with is this:
<a href="url_here"><?php echo esc_html( $eventType ); ?></a>

How do I get all eventType values that were selected?

Thanks!

Can anyone please help me with this? Maybe @aqmiami7 or @apos37 ? Thank yall :slight_smile:

@luna I can’t seem to get the values from the get_{key}() function. The values are listed as terms with the taxonomy name being hp_listing_{key}, so you can fetch them like so:

$eventtypes = get_the_terms( $listing->get_id(), 'hp_listing_eventtype' );
foreach ( $eventtypes as $eventtype ) {
    $name = $eventtype->name;
}
1 Like

Hi, I’ll take a look at the code but maybe as an easier work around you can use the listing tags, as the event type instead. Then display them wherever you like in the template. I’ve done this before and it worked great. Not sure if in your use case it is what you want or not though.

1 Like

Thank you both @aqmiami7 and @apos37 !
I was able to get the name using @apos37 suggestion!
@aqmiami7 I am not sure I understand how that would work. I am still learning about WP and HP. This is an attribute a vendor has to choose when creating a listing , so I am not sure how they would choose that as a tag.

1 Like

Sorry for the delay. You can try using this code:

<?php echo esc_html( $eventType->get_name() ); ?>

Another option is iterating over the names instead of objects if the option name is the only required detail:

<?php foreach ($listing -> get_eventtype__name() as $eventType ) : ?>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.