LLMS_Abstract_Generator_Posts::create_post( string $type, array $raw = array(), int $author_id = null )

Generate a new LLMS_Post_Mdel


Parameters Parameters

$type

(string) (Required) The LLMS_Post_Model post type type. For example "course" for an LLMS_Course or membership for LLMS_Membership.

$raw

(array) (Optional) Array of raw, used to create the post.

Default value: array()

$author_id

(int) (Optional) Fallback author ID, used when now author data can be found in $raw.

Default value: null


Top ↑

Return Return

(LLMS_Post_Model)


Top ↑

Source Source

File: includes/abstracts/llms-abstract-generator-posts.php

	 * @throws Exception When the class identified by `$type` is not found or when an error is encountered during post creation.
	 */
	protected function create_post( $type, $raw = array(), $author_id = null ) {

		$class_name = sprintf( 'LLMS_%s', implode( '_', array_map( 'ucfirst', explode( '_', $type ) ) ) );
		if ( ! class_exists( $class_name ) ) {
			throw new Exception( sprintf( __( 'The class "%s" does not exist.', 'lifterlms' ), $class_name ), self::ERROR_INVALID_POST );
		}

		// Don't create useless revision on "cloning".
		add_filter( 'wp_revisions_to_keep', '__return_zero', 999 );

		// Insert the object.
		$post = new $class_name(
			'new',
			/**
			 * Filter the data used to generate a new post.
			 *
			 * @since 7.4.0
			 *
			 * @param array $new_post_data New post data array.
			 * @param array $raw           Original raw post data array.
			 */
			apply_filters(
				'llms_generator_new_post_data',
				array(
					'post_author'   => $this->get_author_id_from_raw( $raw, $author_id ),
					'post_content'  => isset( $raw['content'] ) ? $raw['content'] : '',
					'post_date'     => isset( $raw['date'] ) ? $this->format_date( $raw['date'] ) : null,
					'post_excerpt'  => isset( $raw['excerpt'] ) ? $raw['excerpt'] : '',
					'post_modified' => isset( $raw['modified'] ) ? $this->format_date( $raw['modified'] ) : null,
					'post_status'   => isset( $raw['status'] ) ? $raw['status'] : $this->get_default_post_status(),
					'post_title'    => $raw['title'],
				),
				$raw
			)
		);

		if ( ! $post->get( 'id' ) ) {
			// Translators: %s = post type name.
			throw new Exception( sprintf( __( 'Error creating the %s post object.', 'lifterlms' ), $type ), self::ERROR_CREATE_POST );


Top ↑

Changelog Changelog

Changelog
Version Description
4.7.1 Set the post's excerpt during the initial insert instead of during metadata updates after creation.
4.7.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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