【发布时间】: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