Support

Account

Home Forums ACF PRO Show post based on value of ACF check box.

Helping

Show post based on value of ACF check box.

  • I’m trying to loop in a custom post type, and show only certain ones based on an ACF5 checkbox field. Can’t get it to work.

    Here’s my code:

    $loop = new WP_Query( array( 'post_type' => 'oil-products','meta_key' => 'type','meta_value' => 'Household','orderby'=>'title','order'=>'ASC','posts_per_page' => 40 ) );
    			    while ( $loop->have_posts() ) : $loop->the_post();
    		   
    			       echo '<div class="oil-products-container">';
    			       echo '<div class="oil-products-image"><a href="';
    			       the_permalink();	    
    			       echo '"><img src="';
    			       the_field('image');          
    			       echo '" alt="';
    			       the_title();
    			       echo '"></a></div>';
    			       echo '<div class="oil-products-title"><a href="';
    			       the_permalink();
    			       echo '" class="black-link">';
    			       the_field('short_name');
    			       echo '</a></div></div>';
    			      endwhile;

    “Type” is the ACF checkbox field. “Household” is the checkbox or value I’m trying to sort on. Anyone know what I’m doing wrong? Thanks!!

  • ACF stores checkbox fields hold an array and these are stored in the database as serialized data so testing the value being = to one of your checkbox values will not work.

    Something like this might work:

    
    $args = array(
        'post_type' => 'oil-products',
        'orderby' => 'title',
        'order' => 'ASC',
        'post_per_page' => 40,
        'meta_query' => array(
            array(
                'key' => 'type',
                'value' => 'Household'
                'compare' => 'LIKE'
            )
        ),
    );
    $loop = new WP_Query($args);
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Show post based on value of ACF check box.’ is closed to new replies.