Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wpsmithtwc/a77b6f2c18d0296bdf77dde713519d75 to your computer and use it in GitHub Desktop.
Save wpsmithtwc/a77b6f2c18d0296bdf77dde713519d75 to your computer and use it in GitHub Desktop.

Revisions

  1. @westonruter westonruter revised this gist Jun 18, 2013. 3 changed files with 3 additions and 0 deletions.
    1 change: 1 addition & 0 deletions wp-meta.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    <?php
    // Copied from core for reference

    /**
    * Retrieve metadata for the specified object.
    1 change: 1 addition & 0 deletions wp-post-thumbnail-template.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    <?php
    // Copied from core for reference

    /**
    * Retrieve Post Thumbnail ID.
    1 change: 1 addition & 0 deletions wp-post.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    <?php
    // Copied from core for reference

    /**
    * Retrieve post meta field for a post.
  2. @westonruter westonruter revised this gist Jun 18, 2013. 1 changed file with 13 additions and 14 deletions.
    27 changes: 13 additions & 14 deletions filter-post-thumbnail-id.php
    Original file line number Diff line number Diff line change
    @@ -4,19 +4,18 @@
    */

    add_filter( 'get_post_metadata', function ( $value, $post_id, $meta_key, $single ) {
    // We want to pass the actual _thumbnail_id into the filter, so requires recursion
    static $is_recursing = false;
    // We want to pass the actual _thumbnail_id into the filter, so requires recursion
    static $is_recursing = false;

    // Only filter if we're not recursing and if it is a post thumbnail ID
    if ( ! $is_recursing && $meta_key === '_thumbnail_id' ) {
    $is_recursing = true; // prevent this conditional when get_post_thumbnail_id() is called
    $value = get_post_thumbnail_id( $post_id );
    $is_recursing = false;
    $value = apply_filters( 'post_thumbnail_id', $value, $post_id ); // yay!
    if ( ! $single ) {
    $value = array( $value );
    }
    }
    return $value;
    // Only filter if we're not recursing and if it is a post thumbnail ID
    if ( ! $is_recursing && $meta_key === '_thumbnail_id' ) {
    $is_recursing = true; // prevent this conditional when get_post_thumbnail_id() is called
    $value = get_post_thumbnail_id( $post_id );
    $is_recursing = false;
    $value = apply_filters( 'post_thumbnail_id', $value, $post_id ); // yay!
    if ( ! $single ) {
    $value = array( $value );
    }
    }
    return $value;
    }, 10, 4);

  3. @westonruter westonruter revised this gist Jun 18, 2013. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion wp-post-thumbnail-template.php
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,6 @@
    * @return int
    */
    function get_post_thumbnail_id( $post_id = null ) {
    $post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
    $post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
    return get_post_meta( $post_id, '_thumbnail_id', true );
    }
    2 changes: 1 addition & 1 deletion wp-post.php
    Original file line number Diff line number Diff line change
    @@ -14,5 +14,5 @@
    * is true.
    */
    function get_post_meta($post_id, $key = '', $single = false) {
    return get_metadata('post', $post_id, $key, $single);
    return get_metadata('post', $post_id, $key, $single);
    }
  4. @westonruter westonruter revised this gist Jun 18, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion filter-post-thumbnail-id.php
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    * How to filter the value that would be returned by get_post_thumbnail_id()
    */

    add_filter( 'get_post_metadata', function ($value, $post_id, $meta_key, $single) {
    add_filter( 'get_post_metadata', function ( $value, $post_id, $meta_key, $single ) {
    // We want to pass the actual _thumbnail_id into the filter, so requires recursion
    static $is_recursing = false;

  5. @westonruter westonruter revised this gist Jun 18, 2013. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions filter-post-thumbnail-id.php
    Original file line number Diff line number Diff line change
    @@ -4,15 +4,18 @@
    */

    add_filter( 'get_post_metadata', function ($value, $post_id, $meta_key, $single) {
    // We want to pass the actual _thumbnail_id into the filter, so requires recursion
    static $is_recursing = false;

    // Only filter if we're not recursing and if it is a post thumbnail ID
    if ( ! $is_recursing && $meta_key === '_thumbnail_id' ) {
    $is_recursing = true;
    $is_recursing = true; // prevent this conditional when get_post_thumbnail_id() is called
    $value = get_post_thumbnail_id( $post_id );
    $value = apply_filters( 'post_thumbnail_id', $value, $post_id );
    $is_recursing = false;
    $value = apply_filters( 'post_thumbnail_id', $value, $post_id ); // yay!
    if ( ! $single ) {
    $value = array( $value );
    }
    $is_recursing = false;
    }
    return $value;
    }, 10, 4);
  6. @westonruter westonruter revised this gist Jun 18, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion filter-post-thumbnail-id.php
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@
    if ( ! $is_recursing && $meta_key === '_thumbnail_id' ) {
    $is_recursing = true;
    $value = get_post_thumbnail_id( $post_id );
    $value = apply_filters( 'the_post_thumbnail_id', $value, $post_id );
    $value = apply_filters( 'post_thumbnail_id', $value, $post_id );
    if ( ! $single ) {
    $value = array( $value );
    }
  7. @westonruter westonruter revised this gist Jun 18, 2013. 4 changed files with 19 additions and 0 deletions.
    19 changes: 19 additions & 0 deletions filter-post-thumbnail-id.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    <?php
    /**
    * How to filter the value that would be returned by get_post_thumbnail_id()
    */

    add_filter( 'get_post_metadata', function ($value, $post_id, $meta_key, $single) {
    static $is_recursing = false;
    if ( ! $is_recursing && $meta_key === '_thumbnail_id' ) {
    $is_recursing = true;
    $value = get_post_thumbnail_id( $post_id );
    $value = apply_filters( 'the_post_thumbnail_id', $value, $post_id );
    if ( ! $single ) {
    $value = array( $value );
    }
    $is_recursing = false;
    }
    return $value;
    }, 10, 4);

    File renamed without changes.
    File renamed without changes.
  8. @westonruter westonruter created this gist Jun 18, 2013.
    52 changes: 52 additions & 0 deletions meta.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    <?php

    /**
    * Retrieve metadata for the specified object.
    *
    * @since 2.9.0
    *
    * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
    * @param int $object_id ID of the object metadata is for
    * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for
    * the specified object.
    * @param bool $single Optional, default is false. If true, return only the first value of the
    * specified meta_key. This parameter has no effect if meta_key is not specified.
    * @return string|array Single metadata value, or array of values
    */
    function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) {
    if ( !$meta_type )
    return false;

    if ( !$object_id = absint($object_id) )
    return false;

    $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single );
    if ( null !== $check ) {
    if ( $single && is_array( $check ) )
    return $check[0];
    else
    return $check;
    }

    $meta_cache = wp_cache_get($object_id, $meta_type . '_meta');

    if ( !$meta_cache ) {
    $meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
    $meta_cache = $meta_cache[$object_id];
    }

    if ( !$meta_key )
    return $meta_cache;

    if ( isset($meta_cache[$meta_key]) ) {
    if ( $single )
    return maybe_unserialize( $meta_cache[$meta_key][0] );
    else
    return array_map('maybe_unserialize', $meta_cache[$meta_key]);
    }

    if ($single)
    return '';
    else
    return array();
    }
    14 changes: 14 additions & 0 deletions post-thumbnail-template.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    <?php

    /**
    * Retrieve Post Thumbnail ID.
    *
    * @since 2.9.0
    *
    * @param int $post_id Optional. Post ID.
    * @return int
    */
    function get_post_thumbnail_id( $post_id = null ) {
    $post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
    return get_post_meta( $post_id, '_thumbnail_id', true );
    }
    18 changes: 18 additions & 0 deletions post.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    <?php

    /**
    * Retrieve post meta field for a post.
    *
    * @since 1.5.0
    * @uses $wpdb
    * @link http://codex.wordpress.org/Function_Reference/get_post_meta
    *
    * @param int $post_id Post ID.
    * @param string $key Optional. The meta key to retrieve. By default, returns data for all keys.
    * @param bool $single Whether to return a single value.
    * @return mixed Will be an array if $single is false. Will be value of meta data field if $single
    * is true.
    */
    function get_post_meta($post_id, $key = '', $single = false) {
    return get_metadata('post', $post_id, $key, $single);
    }