LLMS_Post_Model::toArrayCustom( array $arr )

Called by toArray to add custom fields via get_post_meta()


Description Description

Removes all custom props registered to the $this->properties automatically. Also removes some fields used by the WordPress core that don’t hold necessary data. Extending classes may override this class to exclude, extend, or modify the custom fields for a post type.


Top ↑

Parameters Parameters

$arr

(array) (Required) Existing post array.


Top ↑

Return Return

(array)


Top ↑

Source Source

File: includes/abstracts/abstract.llms.post.model.php

	protected function toArrayCustom( $arr ) {

		// Build an array of keys that are registered or can be excluded as a custom field.
		$props = array_keys( $this->get_properties() );
		foreach ( $props as &$prop ) {
			$prop = $this->meta_prefix . $prop;
		}
		$props[] = '_edit_lock';
		$props[] = '_edit_last';

		// Get all meta data.
		$custom = array();
		foreach ( get_post_meta( $this->get( 'id' ) ) as $key => $vals ) {

			// Skip registered fields or fields 3rd parties want to skip.
			/**
			 * Filters whether the custom field should be excluded in the array representation of the post model
			 *
			 * The dynamic portion of this hook, `$this->model_post_type`, refers to the model's post type. For example "course",
			 * "lesson", "membership", etc...
			 *
			 * @since 3.30.2
			 *
			 * @param boolean         $exclude   Whether the custom field should be excluded. Default is `false`.
			 * @param string          $key       The custom field name.
			 * @param LLMS_Post_Model $llms_post The LLMS_Post_Model instance.
			 */
			if ( in_array( $key, $props, true ) || apply_filters( "llms_{$this->model_post_type}_skip_custom_field", false, $key, $this ) ) {
				continue;
			}

			// Add it.
			$custom[ $key ] = array_map( 'maybe_unserialize', $vals );

		}

		// Add the compiled custom array.
		$arr['custom'] = $custom;

		return $arr;
	}

Top ↑

Changelog Changelog

Changelog
Version Description
3.30.2 Add filter to allow 3rd parties to prevent a field from being added to the custom field array.
3.30.0 Use maybe_unserialize() to ensure array data is accessible as an array.
3.16.11 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.