在管理区域中按自定义字段(ACF)过滤页面

时间:2019-04-05 作者:globdug

我正在尝试通过使用ACF创建的自定义字段筛选管理区域中的页面。

我在这里找到了一个函数(http://www.iamabdus.com/blog/wordpress/filter-custom-posts-by-custom-field/ ) 我根据自己的需要修改了代码。

当我单击“pages”时,选项加载正确,但当我单击“Filter”时,即使过滤器工作正常,我也会收到一条警告:“为foreach()提供的参数无效”。var\\u dump($acf\\u字段)返回NULL。

我希望我解释了这个问题。。。

代码如下:

add_action( \'restrict_manage_posts\', \'wpse45436_admin_posts_filter_restrict_manage_posts\' );

function wpse45436_admin_posts_filter_restrict_manage_posts(){
 $acf_field_name=\'field_5c65759c23c46\';
 $acf_field=get_field_object($acf_field_name);
 var_dump($acf_field);
 $post_type_to_filter=\'page\';
 $type = \'post\';

 if (isset($_GET[\'post_type\'])):
  $type = $_GET[\'post_type\'];
 endif; // isset($_GET[\'post_type\'])

 if ($post_type_to_filter == $type):
  foreach($acf_field[\'choices\'] as $field_value => $field_label){
   $values[$field_label]=$field_value;
  }
  ?>
  <select name="custom_field_filter_value">
   <option value="">Filtra per tipo pagina</option>
   <?php $current_v = isset($_GET[\'custom_field_filter_value\'])? $_GET[\'custom_field_filter_value\']:\'\';
    foreach ($values as $label => $value) :
     printf(
      \'<option value="%s"%s>%s</option>\',
      $value,
      $value == $current_v? \' selected="selected"\':\'\',
      $label
     );
    endforeach;
   ?>
  </select>
  <?php
 endif;
}

add_filter( \'parse_query\', \'wpse45436_posts_filter\' );
function wpse45436_posts_filter( $query ){
 global $pagenow;
 $type = \'post\';
 if (isset($_GET[\'post_type\'])):
  $type = $_GET[\'post_type\'];
 endif;
 $query->query_vars[\'meta_value\'] = $_GET[\'custom_field_filter_value\'];
}

2 个回复
SO网友:Pawan Kumar

尝试使用此代码并修改acf的自定义帖子名和元键,它就会工作。

add_action( \'restrict_manage_posts\', \'wpse45436_admin_posts_filter_restrict_manage_posts\' );
/**
 * First create the dropdown
/** Create the filter dropdown */
function wpse45436_admin_posts_filter_restrict_manage_posts(){
    $type = \'movies\'; // change to custom post name.
    if (isset($_GET[\'movies\'])) {
        $type = $_GET[\'movies\'];
    }

    //only add filter to post type you want
    if (\'movies\' == $type){
        //change this to the list of values you want to show
        //in \'label\' => \'value\' format
        $values = array(
            \'label\' => \'value1\', 
            \'label1\' => \'value2\',
            \'label2\' => \'value3\',
        );
        ?>
        <select name="ADMIN_FILTER_FIELD_VALUE">
        <option value=""><?php _e(\'Filter By \', \'wose45436\'); ?></option>
        <?php
            $current_v = isset($_GET[\'ADMIN_FILTER_FIELD_VALUE\'])? $_GET[\'ADMIN_FILTER_FIELD_VALUE\']:\'\';
            foreach ($values as $label => $value) {
                printf
                    (
                        \'<option value="%s"%s>%s</option>\',
                        $value,
                        $value == $current_v? \' selected="selected"\':\'\',
                        $label
                    );
                }
        ?>
        </select>
        <?php
    }
}


add_filter( \'parse_query\', \'wpse45436_posts_filter\' );
/**
 * if submitted filter by post meta

 * @return Void
 */
function wpse45436_posts_filter( $query ){
    global $pagenow;
    $type = \'movies\'; // change to custom post name.
    if (isset($_GET[\'movies\'])) {
        $type = $_GET[\'movies\'];
    }
    if ( \'movies\' == $type && is_admin() && $pagenow==\'edit.php\' && isset($_GET[\'ADMIN_FILTER_FIELD_VALUE\']) && $_GET[\'ADMIN_FILTER_FIELD_VALUE\'] != \'\') {
        $query->query_vars[\'meta_key\'] = \'META_KEY\'; // change to meta key created by acf.
        $query->query_vars[\'meta_value\'] = $_GET[\'ADMIN_FILTER_FIELD_VALUE\']; 
    }
}

SO网友:David Rhoderick

因此,我尝试了Pawan的答案,虽然它确实有效,但它将过滤器应用到了所有地方,而不是我想要的自定义帖子类型。因此,我做了一个小小的修改,应该可以使这项工作:

public function filter_exhibitions($post_type, $which) {
  $target = \'exhibition\'; // Created this target variable and removed the $_GET stuff

  if($post_type == $target) { // Test for correct post type
    $values = array(
      \'Temporary\' => \'temporary\', // Add your own \'Label\' => \'value\' pairs here
      \'Permanent\' => \'permanent\',
      \'Online\'      => \'online\',
    );

    ?>
    <select name="ADMIN_FILTER_FIELD_VALUE">
      <option value=""><?php _e(\'All exhibition types\', \'artlytical-media\'); // Change this to the default null value display ?></option>
    <?php
    $current_v = isset($_GET[\'ADMIN_FILTER_FIELD_VALUE\'])? $_GET[\'ADMIN_FILTER_FIELD_VALUE\']:\'\';
    foreach ($values as $label => $value) {
      printf(
        \'<option value="%s"%s>%s</option>\',
        $value,
        $value == $current_v? \' selected="selected"\':\'\',
        $label
      );
    }
    ?>
    </select>
    <?php
  }
}
add_action( \'restrict_manage_posts\', \'filter_exhibitions\' );

function exhibition_posts_filter($query){
  global $pagenow;

  if(is_admin()) {
    $type = $query->query[\'post_type\'];
    $target = \'exhibition\';

    if($type == $target &&
      $pagenow == \'edit.php\' &&
      isset($_GET[\'ADMIN_FILTER_FIELD_VALUE\']) &&
      $_GET[\'ADMIN_FILTER_FIELD_VALUE\'] != \'\') {
      $query->query_vars[\'meta_key\'] = \'exhibition_type\'; // Change to meta key created by acf
      $query->query_vars[\'meta_value\'] = $_GET[\'ADMIN_FILTER_FIELD_VALUE\']; 
    }
  }
}

add_filter( \'parse_query\', \'wpse45436_posts_filter\' );

相关推荐

可以为一个wp网站创建两个不同的wp-admin吗

我只是想知道是否有可能创建一个网站,例如newwebsite。com并创建两个wp管理员,每个管理员具有不同的url,例如一个具有newwebsite。com/wp admin和一个as newwebsite。com/dashboard或类似的东西,或者我需要为其注册另一个子域。我的目标-我的目标实际上是,我想创建一个网站,其他用户可以在我的网站上做某些事情,比如写帖子,但创建项目,并在我的网站上显示它们,他们将使用新网站。com/仪表板。