Support

Account

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

Solved

Get Post/Pages based on ACF checkbox

  • Hello.

    i’m new on ACF but i like it so much.
    i want to get posts OR pages based on ACF field.
    the field type is checkbox. means i want to get X random posts / pages if one of the values of checkbox was in the current post / page. actually i want create a related post ( but not with terms, with ACF checkbox ).

    i tried many codes specially in the ACF support forum.
    anything will help me.

    thanks

  • Hi @elliot

    Thanks for responding.
    actually i code it before opening this support thread. my code is almost same as the above thread you linked but my problem is it’s not working with multiple values. means when the page / post contain multiple values ( actually it’s an array of values ) the relation code not working, and the above code is working only with two values but what if i have more than two values ?

    i have an ACF checkbox with many values ( like value1, value2, value3, … , value10 ). the above code may works fine for one or two value but how i should loop it to work with the array.

    also this is my code:

    	$checkbox_field = get_field( 'checkbox_field', $post->ID );
    	$checkbox_field_opts = implode( ", ", $checkbox_field);
    	$args = array(
    		'post_type'				=> 'page',
    		'post__not_in'			=> array( $post->ID ),
    		'posts_per_page'		=> $related_no,
    		'meta_query'			=> array(
    			'relation'		=> 'OR',
    			array(
    				'key'		=> 'checkbox_field',
    				'value'		=>	$checkbox_field_opts,
    				'compare'	=>	'LIKE'
    				)
    			)
    		);

    i tried to implode it, but that’s not working too.

    Thanks
    A

  • Hi @astrixoblix

    Just to clarify, you are loading the current checkbox value via get_field, then you want to find all other posts which have exactly the same checkbox values? Or are you finding any posts which have any of the checkbox values ticked?

    I think you need to dynamicaly generate the meta_query args array by loop through your checkbox values and appending to the meta_query args array.

    Does that help?

    Thanks
    E

  • Hi @elliot

    Yes. i’m getting the values of current post’s checkbox via get_field, and i’m trying to find any pages which have any of current checkbox values.
    as i said, i’m trying to create something like ‘related post’ but via ACF checkbox.

    i’m not sure how i should dynamically generate the meta_query args array.

    i tried this code:

    	$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',
    			array(
    				'key'		=> 'checkbox_field',
    				'value'		=>	$checkbox_field ,
    				'compare'	=>	'IN'
    				)
    			)
    		);

    didn’t worked.
    removing ‘relation’ => ‘AND’, adding meta_key does not have any effect on result.

    i searched whole google, but didn’t find any solution for my problem. how i should do that :S

    Thanks,
    A

  • 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

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

The topic ‘Get Post/Pages based on ACF checkbox’ is closed to new replies.