Php ACF筛选器从选择中排除所有类别

Php ACF筛选器从选择中排除所有类别,php,wordpress,filter,advanced-custom-fields,Php,Wordpress,Filter,Advanced Custom Fields,我正在为WordPress使用“前端提交专业版”和“ACF(非专业版)”插件。 我正在使用这些插件为我的用户制作前端PostCreator 我有200多个类别,所以我想让我的用户更容易选择类别。我将创建多个表单,每个表单都有几个类别可从用户中选择 现在,我使用下面的过滤器从表单中排除一些类别 add_filter('acf/fields/taxonomy/query/name=kathgories', 'exclude_categories', 10, 2); function exclude

我正在为WordPress使用“前端提交专业版”和“ACF(非专业版)”插件。 我正在使用这些插件为我的用户制作前端PostCreator

我有200多个类别,所以我想让我的用户更容易选择类别。我将创建多个表单,每个表单都有几个类别可从用户中选择

现在,我使用下面的过滤器从表单中排除一些类别

add_filter('acf/fields/taxonomy/query/name=kathgories', 'exclude_categories', 10, 2);

function exclude_categories( $args, $field ) {
    global $uncategorized_id;
    $args['exclude'] = array(290,287,283,289,281,291,286,280,284,279); //the IDs of the excluded terms
    return $args;
}
因为我有很多类别,我不能在上面的代码中排除200个类别,这太难了

因此,我需要一个过滤器,它将排除所有类别,只包括我希望在每个表单中显示的5-10个类别。 我不知道该怎么做,所以我想问是否有人能帮忙


我还希望每个筛选器只应用于一个表单。我需要某种方法将筛选器链接到正确的表单。(可能通过链接或表单名称)

使用筛选器解决此问题。

add_action( 'init', 'get_term_ids' );
function get_term_ids() {
    global $uncategorized_id;
    $u = get_term_by( 'slug', 'uncategorized', 'product_cat' );
    $uncategorized_id = $u->term_id;
}

add_filter('acf/fields/taxonomy/query/name=kathgories', 'exclude_categories', 10, 2);
function exclude_categories( $args, $field ) {
  global $uncategorized_id;
  $args['exclude'] = array($uncategorized_id); //the IDs of the excluded terms
  return $args;
}

回答我的问题

我在上面的过滤器中将“排除”改为“包括”,它似乎完全符合我的要求

它只显示我在表单中给出的类别

对于在“选择”中显示类别的用户,请使用以下代码

add_filter('acf/fields/taxonomy/query/name=kathgories', 'include_categories', 10, 2);

function include_categories( $args, $field ) {
      global $uncategorized_id;
      $args['include'] = array(290,287,283,289,281,291,286,280,284,279); //the IDs of the excluded terms
      return $args;
}
add_filter('acf/fields/taxonomy/wp_list_categories/name=kathgories', 'my_taxonomy_args', 10, 2);

function my_taxonomy_args( $args, $field ){
      $args['include'] = array(197,247,245,250,246,248,249,251);//the IDs of the excluded terms
      return $args;
}
对于在“复选框”中显示类别的用户,请使用以下代码

add_filter('acf/fields/taxonomy/query/name=kathgories', 'include_categories', 10, 2);

function include_categories( $args, $field ) {
      global $uncategorized_id;
      $args['include'] = array(290,287,283,289,281,291,286,280,284,279); //the IDs of the excluded terms
      return $args;
}
add_filter('acf/fields/taxonomy/wp_list_categories/name=kathgories', 'my_taxonomy_args', 10, 2);

function my_taxonomy_args( $args, $field ){
      $args['include'] = array(197,247,245,250,246,248,249,251);//the IDs of the excluded terms
      return $args;
}
同时更改
/name=kathgories'
使用您自己的ACF分类字段名


因此,通过这一更改,我回答了我的第二个问题。

此筛选器与我发布的筛选器具有相同的功能。我希望默认情况下排除所有类别,并且只包含少数类别。与上述筛选器相反,请更改代码。请继续使用此代码并告诉我您的想法。我认为您的代码仅排除所有类别在我想要的类别之后没有找到包含的方法。我尝试了你的代码,但不适用于我。不从表单中排除任何类别。