Support

Account

Home Forums General Issues How to select specific posts of a custom post type in a loop with ACF?

Solving

How to select specific posts of a custom post type in a loop with ACF?

  • Hi,

    I have a custom post type (speakers) for my conferences and I am using the amazing ACF plug-in for my custom fields for my speakers and I am using a loop in a page template to show my speakers and their information and things are working perfectly fine.

    Now, I need to create some speakers for the workshop and I don’t want to create a new custom post type with new custom fields and a new page template and single page etc. etc.

    I prefer to use the same custom post type and simply add a checkbox (is workshop speaker) as a new field, so if the checkbox is checked then I want to have a loop that only filters the speakers that are checked as workshop speaker.

    This is how my loop looks like now:

    
    <?php
    $args = array( 'post_type' => 'speakers', 'posts_per_page' => 100, 'orderby' => 'title', 'order' => 'ASC' );
    $loop = new WP_Query( $args );
    
    while ( $loop->have_posts() ) : $loop->the_post();
    
    	echo '<div class="entry-content">';
    		
    		echo '<h2 class="speaker-name">';
    			
    			echo '<img src="' . get_field('speaker_headshot') . '" />';
    			
    			the_title();
    			
    			echo ' <span class="presenter-type">';
    				the_field('presenter_type');
    			echo '</span>';
    			
    			echo '<br/><span class="speaker-title">';
    				the_field('title'); 
    			echo '</span>';
    			
    			echo '<br/><span class="speaker-company">';
    				the_field('company_name');
    			echo '</span>';
    		
    		echo '</h2>';
    		
    			the_content();      
    	
    	echo '</div>';
    
    endwhile;
    ?>
    

    Thank you

  • I kinda figured this out. I added a new True/False custom field to my ‘speakers’ custom post type. Now if the box is checked, then that speaker shows up (in a separate page template for Workshop speakers). I added this to my loop:

    'meta_query' => array(
    	array (
    		'key' => 'is_workshop_speaker',
    		'value' => '1',
    		'compare' => '=='
    		)
    	)

    Now, only the True/False (checked) speakers are showing on my ‘Workshop Speakers‘ page which is perfect but those that are checked are still showing on my ‘Conference Speakers‘ page as well. Now my question is, how to exclude those that are checked as Workshop Speaker from my Conference Speakers page?

    Thanks

  • Hi @Marzo

    Perhaps you need to use similar code on your Conference Speakers page as well?

    Thanks
    E

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘How to select specific posts of a custom post type in a loop with ACF?’ is closed to new replies.