I’ve searched high and low for an answer on the forums and a general Google search, but have yet to solve this little problem.
I’m working on a site (not created by me) that has a custom post type of ‘testimonial’. The client wants to add specific testimonials to a page, so I’m using ACF to create a custom field for ‘Category’ that will allow display of testimonials on a page within a certain category.
My ACF settings:
Field name: testimonial_category
Field type: Checkbox
Required: Yes
Choices: home : Home
business : Business
Default value: (no default value set)
The page displaying the testimonials calls the testimonials PHP file via ‘php include’. My code in that file is:
<?php
// args
$args = array(
'post_type' => 'testimonial',
'posts_per_page' => 20,
'key' => 'testimonial_category',
'value' => 'home'
);
// query
$wp_query = null;
global $wp_query;
$wp_query = new WP_Query( $args );
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
I’m trying to get ONLY the custom posts within the ACF ‘home’ custom field. It currently is showing *all* testimonials. I’ve tried different parameters for both the key (such as the field_name in ACF) and value (such as putting ‘home’ in quotes- ‘”home”‘) without success.
What am I missing?
I figured it out – simply put, I went waaaay off the tracks when I tried this initially but failed due to a conflict with custom functions in the theme, which I discovered in troubleshooting this issue. Had I known about the conflict I wouldn’t have been trying alternative methods and forgotten about the serialized array.
Since the value for the category name is a serialized array, a comparator was required. Here’s my array code, if it might help anyone:`$args = array(
‘post_type’ => ‘testimonial’,
‘posts_per_page’ => 20,
‘meta_query’ => array(
array(
‘key’ => ‘tcat’, // from ACF custom field name
‘value’ => ‘”business”‘, // from ACF custom field checkbox options
‘compare’ => ‘LIKE’ // to match the string from the serialized ACF array
)
)
);
`