has_meta() WordPress Function

The has_meta() function is used to check if a post has any custom fields. This function can be useful if you need to know if a post has any custom fields before running any further code.

has_meta( int $postid ) #

Returns meta data for the given post ID.


Parameters

$postid

(int)(Required)A post ID.


Top ↑

Return

(array[]) Array of meta data arrays for the given post ID.

  • '...$0'
    (array) Associative array of meta data.
    • 'meta_key'
      (string) Meta key.
    • 'meta_value'
      (mixed) Meta value.
    • 'meta_id'
      (string) Meta ID as a numeric string.
    • 'post_id'
      (string) Post ID as a numeric string.


Top ↑

Source

File: wp-admin/includes/post.php

function has_meta( $postid ) {
	global $wpdb;

	return $wpdb->get_results(
		$wpdb->prepare(
			"SELECT meta_key, meta_value, meta_id, post_id
			FROM $wpdb->postmeta WHERE post_id = %d
			ORDER BY meta_key,meta_id",
			$postid
		),
		ARRAY_A
	);
}


Top ↑

Changelog

Changelog
VersionDescription
1.2.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More