dev-master
9999999-devThis package helps fetch data from the WordPress postmeta table.
GNU GPL-3.0-only
by David Egan
This package helps fetch data from the WordPress postmeta table.
Access WordPress data held in the postmeta table., (*1)
Run:, (*2)
composer require carawebs/wp-metadata-accessor
From within theme files, instantiate Carawebs\DataAccessor\PostMetaData
., (*3)
You can then easily return field data by means of the PostMetaData::getField()
method., (*4)
PostMetaData::getField( string $fieldName, string $filter)
Sample usage:, (*5)
$postMeta = new Carawebs\DataAccessor\PostMetaData; // returned content, unfiltered $extra_content = $postMeta->getField('extra_content'); // returned content, filtered by `esc_html()` $intro_text = $postMeta->getField('intro_text', 'text'); // returned content, filtered by `esc_html()` $intro_text = $postMeta->getField('intro_text', 'esc_html'); // returned content filtered by WordPress 'the_content' filter $extra_content = $postMeta->getContentField('extra_content');
The PostMetaData::getField()
method accepts an optional string denoting the field type as a second parameter. This is used to determine the filtering method that should be applied to the returned data., (*6)
If you're using ACF fields you don't specify a field type - this is determined automatically., (*7)
Parameter | Filter or Method Used on $content
|
---|---|
"relationship" |
Data::relationship(array $postIDs, $returnFormat = NULL) : returns either an array of post IDs or customised post objects |
"image" |
Data::image($id, $returnFormat = NULL) : returns either an image data array, object, url or ID |
"esc_html" | esc_html($content) |
"text" | esc_html( $content ) |
"esc_url" | esc_url( $content ) |
"the_content" | apply_filters( 'the_content', $content ) |
"wysiwyg" | apply_filters( 'the_content', $content ) |
"date" | date( 'M j, Y', strtotime( esc_html( $content ) ) ) |
"float" | (float)$content |
"int" | (int)$content |
"OEmbed" | None |
"object" | None |
Unrecognized string | wp_kses_post($content) |
Fetch repeater field data from post_meta table. Returns subfield data grouped by "row" into arrays., (*8)
ACF repeater field data is stored in the postmeta table as a collection of records. Repeater fields allow the editor to add as many groups of subfields as necessary., (*9)
The repeater field key returns an integer value that represents the number of repeater field records - this allows each record to have a unique index. The subfields are created by concatenating the repeater field name with the index and the subfield name. This allows as many items as necessary to be added. In key => value notation, the data collection looks like this:, (*10)
$repeater => $count
$first_subfield => $repeater . '_' . $index . '_' . $first_subfield
$second_subfield => $repeater . '_' . $index . '_' . $second_subfield
// etcTo use:, (*11)
$postMeta = new Carawebs\DataAccessor\PostMetaData; $carouselSubfields = [ 'image' => ['image_ID', 'full'], // denotes an image ID subfield, image size to return 'description' => 'text' // subfield name, filter to apply ]; $carouselData = $postMeta->getRepeaterField('slider', $carouselSubfields);
The field type 'relationship' fetches an array of post IDs., (*12)
For an ACF relationship field, $this->postMeta->getField('related_posts')
...will return type based on that specified in the ACF field GUI., (*13)
Otherwise, you can pass in a field name and specify the 'relationship' type and an array of post IDs will be returned: $this->postMeta->getField('related_posts', 'relationship')
., (*14)
If you're using ACF fields and the field 'return_format' is set to return an object, a modified WordPress post object with additional properties representing the post featured image and permalink will be returned. This Object is filtered with 'carawebs/wp-metadata-accessor/post-object'
. To use this filter, add something like this within the active theme:, (*15)
add_filter('carawebs/wp-metadata-accessor/post-object', function($obj, $id) { $obj->newProperty = someFunction($id); return $obj; }, 1, 2);
This package helps fetch data from the WordPress postmeta table.
GNU GPL-3.0-only