wpseek.com
A WordPress-centric search engine for devs and theme authors



get_feed_build_date › WordPress Function

Since5.2.0
Deprecatedn/a
› get_feed_build_date ( $format )
Parameters:
  • (string) $format Date format string to return the time in.
    Required: Yes
Returns:
  • (string|false) The time in requested format, or false on failure.
Defined at:
Codex:

Gets the UTC time of the most recently modified post from WP_Query.

If viewing a comment feed, the time of the most recently modified comment will be returned.


Source

function get_feed_build_date( $format ) {
	global $wp_query;

	$datetime          = false;
	$max_modified_time = false;
	$utc               = new DateTimeZone( 'UTC' );

	if ( $wp_query instanceof WP_Query && is_array( $wp_query->posts ) ) {
		/*
		 * Collect the post modified times. WP_Post objects are read as-is, since
		 * passing them through get_post() would look them up again by ID, which
		 * drops virtual posts and discards in-memory changes. Queries using
		 * fields => 'ids' (or 'id=>parent') yield post IDs instead, which are
		 * looked up after priming the post cache to avoid a query per post.
		 */
		$modified_times = array();
		$post_ids       = array();
		foreach ( $wp_query->posts as $post ) {
			if ( $post instanceof WP_Post ) {
				$modified_times[] = $post->post_modified_gmt;
			} elseif ( is_int( $post ) ) {
				// A post ID, from a query with fields => 'ids'.
				$post_ids[] = $post;
			} elseif ( is_object( $post ) && isset( $post->ID ) && is_int( $post->ID ) ) {
				/*
				 * A partial post object of shape object{ ID: int, post_parent: int },
				 * from a query with fields => 'id=>parent'. It lacks post_modified_gmt,
				 * so the post is looked up by its ID.
				 */
				$post_ids[] = $post->ID;
			}
		}

		if ( $post_ids ) {
			_prime_post_caches( $post_ids, false, false );
			foreach ( $post_ids as $post_id ) {
				$post = get_post( $post_id );
				if ( $post instanceof WP_Post ) {
					$modified_times[] = $post->post_modified_gmt;
				}
			}
		}

		// If this is a comment feed, check those objects too.
		if ( $wp_query->is_comment_feed() && is_array( $wp_query->comments ) ) {
			foreach ( $wp_query->comments as $comment ) {
				// A 'get_comment' filter may have returned null for a comment.
				if ( $comment instanceof WP_Comment ) {
					$modified_times[] = $comment->comment_date_gmt;
				}
			}
		}

		// Determine the maximum modified time.
		if ( $modified_times ) {
			$datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', max( $modified_times ), $utc );
		}
	}

	if ( false === $datetime ) {
		// Fall back to last time any post was modified or published.
		$datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', get_lastpostmodified( 'GMT' ), $utc );
	}

	if ( false !== $datetime ) {
		$max_modified_time = $datetime->format( $format );
	}

	/**
	 * Filters the date the last post or comment in the query was modified.
	 *
	 * @since 5.2.0
	 *
	 * @param string|false $max_modified_time Date the last post or comment was modified in the query, in UTC.
	 *                                        False on failure.
	 * @param string       $format            The date format requested in get_feed_build_date().
	 */
	return apply_filters( 'get_feed_build_date', $max_modified_time, $format );
}