Support

Account

Home Forums Front-end Issues Get Post/Pages based on ACF checkbox Reply To: Get Post/Pages based on ACF checkbox

  • Hi @astrixoblix

    You will need to populate the meta_query args based on the checkbox value you have. Something like this should work:

    
    <?php 
    
    $checkbox_field = get_field( 'checkbox_field', $post->ID );
    
    $args = array(
    	'post_type'			=> 'page',
    	'post__not_in'		=> array( $post->ID ),
    	'posts_per_page'	=> $related_no,
    	'meta_query'		=> array(
    		'relation'			=> 'OR',
    	)
    );
    
    if( $checkbox_field )
    {
    	foreach( $checkbox_field as $v )
    	{
    		$args['meta_query'][] = array(
    			'key'		=> 'checkbox_field',
    			'value'		=> '"' . $v . '"',
    			'compare'	=> 'LIKE'
    		);
    	}
    }
    
    // test args
    echo '<pre>';
    	print_r($args);
    echo '</pre>';
    die;
    
    ?>
    

    Hope that helps.

    Thanks
    E