Support

Account

Home Forums General Issues Multi Select – Get all choices for Query Args Reply To: Multi Select – Get all choices for Query Args

  • I was yesterday recommend to use something similar:

    'tax_query' => [
            'taxonomy'         => 'event_category',
            'include_children' => false,
            'field'            => 'name',
            'terms'            => block_value('event-category')->name,
        ], 

    So to work with ACF fields I would then use

    <?php
    /**
     * Events CPT Block
     *
    **/
    
    // Create id attribute allowing for custom "anchor" value.
    $id = 'pm-' . $block['id'];
    if( !empty($block['anchor']) ) {
    	$id = $block['anchor'];
    }
    
    // Create class attribute allowing for custom "className" and "align" values.
    $className = 'block-pm';
    if( !empty($block['className']) ) {
    	$className .= ' ' . $block['className'];
    }
    if( !empty($block['align']) ) {
    	$className .= ' align' . $block['align'];
    }
    
    // Variables
    $number_of_events = get_field( 'number_of_events' );
    // print_r($number_of_events);
    $event_object = get_field_object('events_category');
    // print_r($event_object);
    $event_cat_name = $event_object['value'];
    $event_cat_slug = $event_cat_name[0]->slug;
    // print_r($event_cat_slug);
    
    $args = array(
        'post_type' => 'event',
        'posts_per_page' => $number_of_events,
        // 'event_category' => $event_cat_slug
        'tax_query' => [
            'taxonomy'         => 'event_category',
            'include_children' => false,
            'field'            => 'name',
            'terms'            => $event_cat_slug, // 
        ], 
    );
    $the_query = new WP_Query( $args ); ?>
    
    <?php if ( $the_query->have_posts() ) : ?>
    
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <h2><?php the_title(); ?></h2>
        <?php endwhile; ?>
    
        <?php wp_reset_postdata(); ?>
    
    <?php endif; ?>

    not sure yet how to get term ids loaded from the events_category field. So far I have only been able to get arrays:

    print_r($event_cat_name);
    Array ( [0] => WP_Term Object ( [term_id] => 179 [name] => Virtual [slug] => virtual [term_group] => 0 [term_taxonomy_id] => 179 [taxonomy] => event_category [description] => [parent] => 0 [count] => 3 [filter] => raw [meta] => Array ( ) ) [1] => WP_Term Object ( [term_id] => 180 [name] => Local [slug] => local [term_group] => 0 [term_taxonomy_id] => 180 [taxonomy] => event_category [description] => [parent] => 0 [count] => 1 [filter] => raw [meta] => Array ( ) ) )
    

    or the first slug of slugs given by chosen event categories:

    print_r($event_cat_slug);
    virtual

    I can switch from term object to term id. But still, how to load the term ids chosen in a variable I can add to terms in the tax_query? Any suggestions?