Home › Forums › Backend Issues (wp-admin) › How to get all ACF groups or field for specific post type › Reply To: How to get all ACF groups or field for specific post type
posted this on another thread but might also be what you’re looking for
function get_acf_fields_post_type( $post_type )
{
global $wpdb;
$sql = "SELECT p.ID, p.post_title, p.post_name, pm.meta_value as rule
FROM $wpdb->posts p
LEFT JOIN $wpdb->postmeta pm
ON ( p.ID = pm.post_id AND pm.meta_key = 'rule' )
WHERE p.post_type = 'acf'";
$result = $wpdb->get_results($sql);
$groups = array();
foreach($result as $row){
$rule = unserialize($row->rule);
if( $rule['param'] == 'post_type' && $rule['operator'] == '==' && $rule['value'] == $post_type )
{
$groups[$row->ID] = array('title'=>$row->post_title,'name'=>$row->post_name);
}
}
foreach($groups as $post_id => $data)
{
$fsql = "SELECT * FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key LIKE 'field_%';";
$fields = $wpdb->get_results($fsql);
$field_array = array();
foreach($fields as $field)
{
$f = unserialize($field->meta_value);
$field_array[$field->meta_key] = $f;
}
$groups[$post_id]['fields'] = $field_array;
}
return $groups;
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.