[wp-trac] [WordPress Trac] #51787: Introduce dedicated function that retrieves post object by metadata
WordPress Trac
noreply at wordpress.org
Mon Nov 16 13:39:13 UTC 2020
#51787: Introduce dedicated function that retrieves post object by metadata
-------------------------------+-----------------------------
Reporter: DaveyJake | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Posts, Post Types | Version: 5.5.3
Severity: normal | Keywords: dev-feedback
Focuses: |
-------------------------------+-----------------------------
Just wanted to suggest a function I routinely use when building custom
themes that utilize metadata.
{{{#!php
<?php
/**
* Get post object from metadata.
*
* @since 5.5.3
*
* @see get_posts()
*
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value.
* @param string $post_type Post type slug. Default 'post'.
*
* @return WP_Post|bool Post object if successful. False if not.
*/
function get_post_by_meta( $meta_key = '', $meta_value = '', $post_type =
'post' ) {
$args = array(
'post_type' => $post_type,
'meta_key' => $meta_key,
'meta_value' => $meta_value,
'meta_compare' => '=',
);
$posts = get_posts( $args );
if ( ! empty( $posts[0] ) ) {
return $posts[0];
}
return false;
}
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/51787>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list