Support

Account

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

  • Hi @rhand

    If your ACF field Event Categories is a taxonomy and its set to return the Term Object (and you have Load and Save terms selected), replace:
    $event_cat_ids = array( 37, 47 );
    With:

    $event_categories = get_field('event_categories');
    $event_cat_ids = wp_list_pluck( $event_categories, 'term_id' );

    So now looks like:

    <?php
    $event_categories = get_field('event_categories');
    $event_cat_ids = wp_list_pluck( $event_categories, 'term_id' );
    
    #$event_cat_ids = array( 1, 31 );
    
    $args = array(
    		'post_type' 		=> 'event',
    		'post_status'		=> 'publish',
    		'posts_per_page'	=> -1, // show all posts.
    		'order'				=> 'DESC',
    );
    			
    $args['tax_query'] = array(
    	'relation' 		=> 'OR', 
    
    	array(
    		'taxonomy'	=> 'event_category',
    		'field'		=> 'term_id',
    		'terms'		=> $event_cat_ids
    	),			
    		
    );
    
    $query = new WP_Query( $args );
    if( $query->have_posts() ) : 
    
    	while( $query->have_posts() ): $query->the_post(); ?>
    	<p><?php the_title(); ?></p>
    	<?php endwhile;  wp_reset_postdata();
    
    endif;
    ?>

    I’ve tested this code and can confirm it works, returning the posts of the selected categories in your multi select