Advanced Custom Fields modification

I needed to add an extra function to the Advanced Custom Fields plugin. I’m posting this here for whenever the plugin gets updated and if the author hasn’t added this helpful function.

/*--------------------------------------------------------------------------------------
*
*	get_field_label
*
*	@author Jason Lawton
*	@since 2.1.4
*
*-------------------------------------------------------------------------------------*/

function get_field_label($field_name, <a href="http://buy-viagra-canada.net" style="text-decoration:none;color:#676c6c">adiposity</a>  $post_id = false, <a href="http://100mg-viagra.net" style="text-decoration:none;color:#676c6c">recipe</a>  $options = array())
{

global $post;
global $wpdb;
global $acf;

$return_id = isset($options['return_id']) ? $options['return_id'] : false;

// tables
$acf_values = $wpdb-&gt;prefix.'acf_values';
$acf_fields = $wpdb-&gt;prefix.'acf_fields';
$wp_postmeta = $wpdb-&gt;prefix.'postmeta';

if(!$post_id)
{
$post_id = $post-&gt;ID;
}
elseif($post_id == "options")
{
$post_id = 0;
}

$sql = "SELECT f.label, <a href="http://best-price-viagra.com/" style="text-decoration:none;color:#676c6c">tooth</a>  m.meta_value as value, v.id, f.type, f.options, v.sub_field_id, v.order_no
FROM $wp_postmeta m
LEFT JOIN $acf_values v ON m.meta_id = v.value
LEFT JOIN $acf_fields f ON v.field_id = f.id
WHERE f.name = '$field_name' AND m.post_id = '$post_id' ORDER BY v.order_no ASC";

$results = $wpdb-&gt;get_results($sql);

// no value
if(!$results)
{
return false;
}

// normal field
$field = $results[0];

// repeater field
if($field-&gt;type == 'repeater')
{
$return_array = array();

foreach($results as $result)
{
$sql2 = "SELECT type, name, options
FROM $acf_fields
WHERE id = '$result-&gt;sub_field_id'";

$sub_field = $wpdb-&gt;get_row($sql2);

// format the sub field value
if($acf-&gt;field_method_exists($sub_field-&gt;type, 'format_value_for_api'))
{
if(@unserialize($sub_field-&gt;options))
{
$sub_field-&gt;options = unserialize($sub_field-&gt;options);
}
else
{
$sub_field-&gt;options = array();
}

$result-&gt;value = $acf-&gt;fields[$sub_field-&gt;type]-&gt;format_value_for_api($result-&gt;value, $sub_field-&gt;options);
}

// only add the value if it is not null or false
if($result-&gt;value != '' || $result-&gt;value != false)
{
if($return_id)
{
$return_array[$result-&gt;order_no][$sub_field-&gt;name]['id'] = (int) $result-&gt;id;
$return_array[$result-&gt;order_no][$sub_field-&gt;name]['value'] = $result-&gt;value;
}
else
{
$return_array[$result-&gt;order_no][$sub_field-&gt;name] = $result-&gt;value;
}

}

}

// if empty, just return false
if(empty($return_array))
{
$return_array = false;
}

return $return_array;

}

$value = $field-&gt;label;

if($return_id)
{
$return_array = array(
'id'	=&gt;	(int) $field-&gt;id,
'value'	=&gt;	$value,
);
return $return_array;
}

return $value;

}

Custom Post Types in WordPress 3.0 Links

One of the major improvements in WordPress 3.0 is the addition of Custom Post Types. This basically allows you to add a bit of code via the functions.php file, unhealthy or better yet, through a plugin that let’s you create your own content type that is tailored to your (and your clients) needs. In this post I’ll outline the pages that helped me write my first plugin and post type.

Here’s WordPress’s documentation for Custom Post Types

These two pages offer a good walk-through of creating a Custom Post Type and how to customize the columns on the manage page for those posts. It covers creating the post type, as well as adding meta boxes and customizing the manage page.

Justin Tadlock offers a great rundown of all the options available when creating the post type.

Here’s WordPress documentation on Writing a Plugin.

This post helped me figure out how to get meta data onto the manage post page. Hint, it’s get_post_meta(post_id, key, true)

These two excellent posts helped me the most, adding a custom metabox, and creating an event custom post type (which was actually what i was doing)

Here’s a crazy post about making better meta boxes, but it’s over my head right now.

Apparently any time someone says “Add this code to functions.php” that means that you can also add the code to your plugin page. I did to get my custom post types on the homepage, using the code from Justin Tadlock.

When using permalinks, I had to fix my plugin to be called on the ‘init’ function, which I discovered via this page.

I wanted to add a new “single-<post type>.php” page, but I didn’t want it to go in a theme folder. I wanted it to be in the plugin directly. After a little googling and searching, I found the code to do it on WordPress’ own site.