WP_Query::generate_postdata( WP_Post|object|int $post ): array|false

In this article

Generates post data.

Parameters

$postWP_Post|object|intrequired
WP_Post instance or Post ID/object.

Return

array|false Elements of post or false on failure.

Source

public function generate_postdata( $post ) {

	if ( ! ( $post instanceof WP_Post ) ) {
		$post = get_post( $post );
	}

	if ( ! $post ) {
		return false;
	}

	$id = (int) $post->ID;

	$authordata = get_userdata( $post->post_author );

	$currentday   = false;
	$currentmonth = false;

	$post_date = $post->post_date;
	if ( ! empty( $post_date ) && '0000-00-00 00:00:00' !== $post_date ) {
		// Avoid using mysql2date for performance reasons.
		$currentmonth = substr( $post_date, 5, 2 );
		$day          = substr( $post_date, 8, 2 );
		$year         = substr( $post_date, 2, 2 );

		$currentday = sprintf( '%s.%s.%s', $day, $currentmonth, $year );
	}

	$numpages  = 1;
	$multipage = 0;
	$page      = $this->get( 'page' );
	if ( ! $page ) {
		$page = 1;
	}

	/*
	 * Force full post content when viewing the permalink for the $post,
	 * or when on an RSS feed. Otherwise respect the 'more' tag.
	 */
	if ( get_queried_object_id() === $post->ID && ( $this->is_page() || $this->is_single() ) ) {
		$more = 1;
	} elseif ( $this->is_feed() ) {
		$more = 1;
	} else {
		$more = 0;
	}

	$content = $post->post_content;
	if ( str_contains( $content, '<!--nextpage-->' ) ) {
		$content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content );
		$content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content );
		$content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content );

		// Remove the nextpage block delimiters, to avoid invalid block structures in the split content.
		$content = str_replace( '<!-- wp:nextpage -->', '', $content );
		$content = str_replace( '<!-- /wp:nextpage -->', '', $content );

		// Ignore nextpage at the beginning of the content.
		if ( str_starts_with( $content, '<!--nextpage-->' ) ) {
			$content = substr( $content, 15 );
		}

		$pages = explode( '<!--nextpage-->', $content );
	} else {
		$pages = array( $post->post_content );
	}

	/**
	 * Filters the "pages" derived from splitting the post content.
	 *
	 * "Pages" are determined by splitting the post content based on the presence
	 * of `<!-- nextpage -->` tags.
	 *
	 * @since 4.4.0
	 *
	 * @param string[] $pages Array of "pages" from the post content split by `<!-- nextpage -->` tags.
	 * @param WP_Post  $post  Current post object.
	 */
	$pages = apply_filters( 'content_pagination', $pages, $post );

	$numpages = count( $pages );

	if ( $numpages > 1 ) {
		if ( $page > 1 ) {
			$more = 1;
		}
		$multipage = 1;
	} else {
		$multipage = 0;
	}

	$elements = compact( 'id', 'authordata', 'currentday', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages' );

	return $elements;
}

Hooks

apply_filters( ‘content_pagination’, string[] $pages, WP_Post $post )

Filters the “pages” derived from splitting the post content.

Changelog

VersionDescription
5.2.0Introduced.

User Contributed Notes

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