【问题标题】:Using ACF Checkbox Field in a custom post type to filter the results of a different custom post type在自定义帖子类型中使用 ACF 复选框字段来过滤不同自定义帖子类型的结果
【发布时间】:2019-11-14 13:54:28
【问题描述】:

我有一个循环的页面(自定义帖子类型 1)。嵌套在(自定义帖子类型 1)内部我有另一个循环(自定义帖子类型 2)。目标是在(自定义帖子类型 1)中选中 ACF 复选框,根据签入的内容(自定义帖子类型 1)过滤(自定义帖子类型 2)的结果。

我是 StackOverflow 和 Wordpress 开发的新手。但我让它按照我上面描述的方式工作,但有一个后退。您必须在(自定义帖子类型 2)中有可用的复选框才能使 $meta_query 起作用。在阅读文档以及我如何进行此设置之后,这是完全有道理的。

我将如何使它(自定义帖子类型 2)不需要复选框以根据(自定义帖子类型 1)选中的复选框进行过滤?

<?php
// Custom Post Type 1
$args1 = array(
    'post_type' => 'custom_post_type_1',
    'posts_per_page' => -1,
);
$custom_post_type_1 = get_posts( $args1 );

foreach( $custom_post_type_1 as $post ) : setup_postdata( $post );
    // Do things

    // Get the selected options from custom post type 1 and throw them into an array                         
    $my_acf_checkbox = get_field('checkbox', $post->ID);
    $meta_query = array('relation' => 'OR');
    foreach( $my_acf_checkbox as $item ){
        $meta_query[] = array(
        'key' => 'checkbox',
        'value' => $item,
        'compare' => 'LIKE',
    );
    }

    // Custom Post Type 2 (Nested)
    $args2 = array(                                      
        'post_type' => 'custom_post_type_2',                                         
        'posts_per_page' => -1,
        'meta_query' => $meta_query
    );                                   
    $custom_post_type_2 = get_posts( $args2 );

    foreach( $custom_post_type_2 as $post ) : setup_postdata( $post );
        // Do things                                     
        wp_reset_postdata();                                     
    endforeach;

    wp_reset_postdata();
endforeach;                             
?>

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    我只是好奇您为什么使用复选框 ACF 字段?返回的值是什么?帖子ID,Slug?我通常会使用 Post Object 字段并返回一个 ID,因此它比将值硬编码到复选框字段中更具动态性。

    我假设您在复选框中返回 post_ids 以获得以下解决方案。

    我可以看到你正在尝试使用 meta_query 做什么,但如果你有 id,你可以简单地在你的 $args2 中使用 post__in 作为第二个循环,而不是这样:

    $my_acf_checkbox = get_field('checkbox', $post->ID);
    $meta_query = array('relation' => 'OR');
        foreach( $my_acf_checkbox as $item ){
        $meta_query[] = array(
            'key' => 'checkbox',
            'value' => $item,
            'compare' => 'LIKE',
        );
    }
    

    只需像这样将您的 id 推送到一个数组中:

    foreach( $my_acf_checkbox as $item ){
        $cpt2s[] = $item;
    }
    

    然后在 $args2 的 post__in 中使用你的新数组:

    $args2 = array(                                      
        'post_type' => 'custom_post_type_2',                                         
        'posts_per_page' => -1,
        'post__in' => $cpt2s
    );
    

    让我知道这是否可以作为您的解决方案,或者如果有什么没有意义。

    【讨论】:

    • 感谢@Paul 的帮助!像魅力一样工作。使用复选框的原因是 UX 设计师的要求。但是在您提到 Post Object 之后,我进一步挖掘以了解 Post Objects,这是使用 Post Object 并返回 ID 的更好途径。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多