Return to Snippet

Revision: 44742
at April 17, 2011 01:49 by prwhitehead


Initial Code
/**
* Usage: Add all_my_customs(); to your page somewhere (header.php is best)
* Usage: if you would like to get the postmeta from a specific page use, all_my_customs('7');
* To access the value <?php echo $_SESSION['pc']['your_custom_name']; ?>
*/

function all_my_customs($id = 0){

	//if a post id is not provided grab the page currently being presented 
	if ($id == 0) 
	{
		global $wp_query;
		$content_array = $wp_query->get_queried_object(); 
		$id = $content_array->ID;
	}

	//grab the customs in their raw form from wp
	$raw = get_post_custom($id);
	//if the raw data is an array run a loop
	if (is_array($raw)) 
	{
		foreach($raw as $key => $value)
		{
			//if the number of items returned for the cf is greater than one (an array of items)
			if (count($value) > 1)
			{
				//retain all
				foreach($value as $v)
				{
					$return[$key][] = $v;	
				}
			}
			//else add the first item
			else
			{
				$return[$key] = $value[0];
			}
		}
		
	}
	//and returns the session array.
	$_SESSION['pc'] = $return;
	return $_SESSION['pc'];
}

Initial URL

                                

Initial Description
Returns all of the custom fields (meta data) from a page / post and assigns them all to a single session array for compactness

Initial Title
Wordpress: Return all custom fields in one session variable

Initial Tags
php, wordpress

Initial Language
PHP