Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Lee Bollu
Lee Bollu
1,168 Points

Advanced Custom Fields ACF - Show posts If Radio "yes"

Hi All,

I am struggling with this one...

I have a radio button in ACF called "sponsored_event". If this is ticked on a post it needs to appear at the top of the page as the most recent sponsored event.

This is my current loop:

<?php
        wp_reset_postdata();
        $myargs = array (
            'showposts' => 1,
            'post_type' => 'event',
            'meta_key' => 'sponsored_event',
            'meta_value' => 'yes'
        );
        $myquery = new WP_Query($myargs);

            if($myquery->have_posts() ) :

                while($myquery->have_posts() ) : $myquery->the_post(); ?>


                            <div class="sponsored-event">
                            <div class="sponsored-image" style="background-image: url(<?php the_field( 'event_image' ); ?>);">
                            </div>
                            <div class="sponsored-info">
                                <h2>Sponsored Event</h2>
                                <h1><strong><?php the_title(); ?></strong></h1>
                                <p><strong>Date</strong></p><br>
                                <p class="place"><?php the_field( 'event_location' ); ?></p>
                                <p class="time"><?php the_field( 'event_time' ); ?></p>
                                <p><?php the_field( 'excerpt' ); ?></p>
                            </div>
                        </div>


                <?php endwhile;
            endif;
        wp_reset_postdata();
    ?>

Any help would be great

Lee

2 Answers

Doru Marginean
Doru Marginean
2,290 Points

Hi Lee Bollu,

I tested your code and it should work. I set the radio buttons in ACF like this:

yes
no

Also I added 'no' as the default value so that when you write a post you don't add it by accident. What exactly Is the issue you are experiencing?

Kevin Korte
Kevin Korte
28,149 Points

I'm assuming that the radio button values must be mapped to "yes" and "no"?

Lee Bollu
Lee Bollu
1,168 Points

Yes they are set to yes and no.