使用ACF复选框值数组来查找“更像这样”

时间:2017-04-07 09:15:38

标签: php advanced-custom-fields

我正在使用ACF复选框为心理学应用/网站指定类别(称为“目标”)。因此,价值观将是“正念”,“幸福”和“抑郁症”。

由于可以选择多个值,我想要做的就是更喜欢这个,任何一篇帖子都可以显示与帖子的一个或多个类别匹配的其他帖子。

更新:这是我现在使用的代码,我仍然没有运气:

the_field( 'aims'); /* since I'm on the page for a single post, this gives me the aims for the current post and is currently returning values from the checkbox with multiple selections (as an array, I believe) */


$args = array(
    'numberposts'   => -1,
    'post_type'     => 'program',
    'post_status'   => 'publish',
    'meta_query'    => array(
        'relation'      => 'AND',
        array(
            'key'       => 'aims',
            'value'     => 'Well-Being', /* I'd actually like this to use the aims for the current post, but I've hard-coded for testing, and am not seeing any results */
            'compare'   => 'IN')
        )); 

$more_like_this = new WP_Query ($args);
while ($more_like_this->have_posts()) : $more_like_this->the_post();

                $star_rating = get_field('overall_score'); 

                ?>
                <?php the_title(); ?>
                <?php echo $star_rating ?>

                <?php endwhile; 
                wp_reset_query();
                ?>

1 个答案:

答案 0 :(得分:0)

如果您需要wp查询来选择一些必须选中多个ACF复选框选项的帖子,您可以这样做:

// Get the array with selected options
$fields = get_field('somefield');

// First create the meta_query variable (AND can also be OR)
$meta_query = array('relation' => 'AND');
foreach( $fields as $field ){
    $meta_query[] = array(
        'key'     => 'name_of_acf_checkbox_here',
        'value'   => $aim,
        'compare' => 'LIKE',
    );
}

// args
$args = array(
    'numberposts'   => -1,
    'post_type'     => 'postype_here',
    'meta_query' => $meta_query,
);

$the_query = new WP_Query( $args );