1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ACFグループを取得してフィールドの設定を一括で表示する

Last updated at Posted at 2017-01-19

ACFは、グループとフィールドで別々のカスタムポストを利用しています。

'post_type' => 'acf-field-group'
'post_type' => 'acf-field',

WP_Query使って表示

$the_query = new WP_Query( array( 'post_type' => 'acf-field-group') );

if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();

  $group_ID = get_the_ID();
  $name = get_the_title();

  echo '<p>Group ID: '.$group_ID.', Group name: '.$name.'</p>';

  $fields = array();
  $fields = apply_filters('acf/field_group/get_fields', $fields, $group_ID);
  //フィールドがなければ0がかえる

  if( $fields ):
    foreach( $fields as $field ):
      $value = get_field( $field['name'] );

      echo '<dl>';
        echo '<dt>' . $field['label'] . '</dt>';
        echo '<dd>' .$value . '</dd>';
      echo '</dl>';
    endforeach;
  endif;
echo '<hr>';
endwhile;
endif;
wp_reset_postdata();

使い勝手を考えれば、ACF管理画面で設定するタイトルをからポスト情報を取り出すのが良いのかも。
ただしタイトルは重複OKなので、運用時に重複させない手間がかかるかも

$group = get_page_by_title('タイトル', OBJECT, 'acf-field-group');

$args = array(
  'post_type' => 'acf-field',
  'post_parent' => $group->ID,
  'posts_per_page' => -1,
  'order' => 'ASC',
  'orderby' => 'menu_order',
);
$posts_total = new WP_Query( $args );

while ( $posts_total->have_posts() ) :
$posts_total->the_post();
$values = $posts_total->post;

echo '<pre>';
echo $values->post_title . '<br>';
echo $values->post_excerpt . '<br>';
echo $values->post_name . '<br>';
echo '</pre>';

endwhile;
wp_reset_postdata();
1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?