Created
January 18, 2016 21:31
-
-
Save craigsimps/18cbcfdbf3ca92d442a6 to your computer and use it in GitHub Desktop.
Class which will retrieve custom fields when passed an array of field names.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Simple class designed to return | |
* multiple custom fields. | |
* | |
* This is a simple class which will | |
* return custom fields in the format | |
* $this->field_name when passed an | |
* array of custom field names. | |
* | |
* @author Craig Simpson | |
* @package | |
* @since 1.0 | |
*/ | |
class Retrieve_Meta { | |
protected $post_id; | |
/** | |
* Retrieve_Meta constructor. | |
* | |
* @param array $fields Array of field names to be assigned and retrieved. | |
* @param int $post_id Post id to be used for lookup, defaults to $post->ID. | |
*/ | |
public function __construct( array $fields, $post_id = 0) { | |
$this->get_meta( $fields ); | |
} | |
/** | |
* Function assigns field name and corresponding value to $this->field. | |
* | |
* @param array $fields Array of field names to be assigned and retrieved. | |
* @param int $post_id Post ID to be used for lookup, defaults to $post->ID. | |
*/ | |
protected function get_meta( array $fields, $post_id = 0 ) { | |
global $post; | |
$this->post_id = $post_id > 0 ? $post_id : $post->ID; | |
foreach( $fields as $field ) { | |
$this->$field = get_post_meta( $this->post_id, $field, true ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment