Support

Account

Home Forums ACF PRO Query posts by checkbox value (dynamically)

Solved

Query posts by checkbox value (dynamically)

  • Hello,

    I have a CPT called “resources”. In the field group assigned to resources I have a checkbox field called “subjects”. On the single template I want to display other “resources” that have at least one of the same checkbox values selected.

    I’ve tried:

    $subjects = get_field('subjects');
    
    $meta_query = array('relation' => 'OR');
    foreach($subjects as $subject) :
    	$meta_query[] = array(
    		'key' => 'subjects',
    		'value' => $subject,
    		'compare' => 'LIKE',
    	);
    endforeach;
    
    $subject_args = array(
    	'post_type' => 'resources',
    	'posts_per_page' => -1,
    	'meta_query' => $meta_query,
    );

    But when I loop through the query, it’s returning all resources posts, even those that don’t have the any of the same checkbox (subjects) checked.

    I’ve also tried:

    $subject_args = array(
    	'post_type' => 'resources',
    	'posts_per_page' => -1,
    	'meta_key' => 'subjects',
            'meta_value' => $subjects, // also tried array($subjects)
    );

    But this is causing and error and breaks the page.

    Any help would be greatly appreciated.

  • Oh I forgot to mention that the return type for subjects is set to value only.

  • It doesn’t matter what the return is, the value stored will only be the values.

    What are the possible values? Are they numbers (1, 2, etc) or text strings with the name of the subject?

  • Hi @hube2 the values are text strings. Here are my choices copied direction from the field group editor:

    agile : Agile
    bom : Basics of Modeling
    gsn : GSN
    mm : Mind Mapping
    safe : SafeML
    se : Systems Engineering
    uml : UML
    uc : Use Cases

  • Honestly, I don’t see why it’s not working, but you can try this

    
    $meta_query = array('relation' => 'OR');
    foreach($subjects as $subject) :
    	$meta_query[] = array(
    		'key' => 'subjects',
    		'value' => '"'.$subject.'"',
    		'compare' => 'LIKE',
    	);
    endforeach;
    
  • @hube2 You da man! That worked! I don’t understand why but I’m not going lose any sleep over it. Thank you very much!!!

  • The information in the field is saved as a serialized value. Something in your subject names was matching seiralization information, but I don’t know what. So instead of looking for %gsn% or whatever looking for %"gsn"% corrects it.

  • The above should not have fixed the issue. There should be no overlaps of serialization information and your checkbox values. Are you sure you’re getting the correct posts? Or are you just not getting all of them now?

    What does this produce

    
    $subjects = get_field('subjects');
    var_dump($subjects);
    
  • I don’t know what is going on. I checked the change I made per your advice and it is retrieving the correct posts. It’s grabbing all the ones that have at least one of the same checkbox values while omitting those that didn’t.

    But I just tried reverting back to use just $subject rather than '"'.$subject.'"' and now it’s working as it should. Besides adding post__not_id to exclude the current post, everything is the same as it was when it wasn’t working for me. Strange.

  • I probably made some error earlier and didn’t realize it. Anyways, talking with you helped me worked through it. I appreciate it regardless.

  • @John Huebner It most definitely works and I couldn’t find any other way.

    I tried creating a simple Yes/No checkbox. I created a field type “Checkbox” and entered the choices “Yes” and “No”. This doesn’t make a single checkbox like you would think. It makes a “Yes” checkbox and a “No” checkbox. What I wanted was a checkbox where true = “Yes” and false = “No”. Instead, I have to uncheck “Yes” and check “No” to change the saved value. The values display correctly everywhere on my site, but filtering is a problem.

    The values stored in the DB are ‘a:1:{i:0;s:3:”Yes”;}’ and ‘a:1:{i:0;s:2:”No”;}’.

    I’m sure you can see why the compare ‘LIKE’ works with the value ‘Yes’.

    In retrospect, I should have just used a radio button.

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

The topic ‘Query posts by checkbox value (dynamically)’ is closed to new replies.