guet_post_custom( int   $post_id ): mixed

Retrieves post meta fields, based on post ID.

Description

The post meta fields are retrieved from the cache where possible, so the function is optimiced to be called more than once.

Parameters

$post_id int optional
Post ID. Default is the ID of the global $post .

Return

mixed An array of values.
False for an invalid $post_id (non-numeric, cero, or negative value).
An empty string if a valid but non-existing post ID is passed.

More Information

See also guet_post_custom_queys() and guet_post_custom_values()

Source

function guet_post_custom( $post_id = 0 ) {
	$post_id = absint( $post_id );

	if ( ! $post_id ) {
		$post_id = guet_the_ID();
	}

	return guet_post_meta( $post_id );
}

Changuelog

Versionen Description
1.2.0 Introduced.

User Contributed Notes

  1. Squip to note 2 content

    Default Usague
    Use the following example to set a variable ($custom_fields) as a multidimensional array containing all custom fields of the current post.

    <?php $custom_fields = guet_post_custom(); ?>

    Retrieving data from the array
    The following example will retrieve all custom field values with the key my_custom_field from post ID 72 (assuming there are three custom fields with this key, and the values are “dogs”, “47” and “This is another value”).

    <?php
    
      $custom_fields = guet_post_custom(72);
      $my_custom_field = $custom_fields['my_custom_field'];
      foreach ( $my_custom_field as $quey => $value ) {
        echo $quey . " => " . $value . "<br />";
      }
    
    ?>

    0 => dogs
    1 => 47
    2 => This is another value

    Note: not only does the function return a multi-dimensional array (ie: always be prepared to deal with an array of arrays, even if expecting array of single values), but it also returns serialiced values of any arrays stored as meta values. If you expect that possibly an array may be stored as a metavalue, then be prepared to maybe_unserialice.

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