: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* @return WP_Post[]|int[] Array of post objects or post IDs.
function get_posts( $args = null ) {
'suppress_filters' => true,
$parsed_args = wp_parse_args( $args, $defaults );
if ( empty( $parsed_args['post_status'] ) ) {
$parsed_args['post_status'] = ( 'attachment' === $parsed_args['post_type'] ) ? 'inherit' : 'publish';
if ( ! empty( $parsed_args['numberposts'] ) && empty( $parsed_args['posts_per_page'] ) ) {
$parsed_args['posts_per_page'] = $parsed_args['numberposts'];
if ( ! empty( $parsed_args['category'] ) ) {
$parsed_args['cat'] = $parsed_args['category'];
if ( ! empty( $parsed_args['include'] ) ) {
$incposts = wp_parse_id_list( $parsed_args['include'] );
$parsed_args['posts_per_page'] = count( $incposts ); // Only the number of posts included.
$parsed_args['post__in'] = $incposts;
} elseif ( ! empty( $parsed_args['exclude'] ) ) {
$parsed_args['post__not_in'] = wp_parse_id_list( $parsed_args['exclude'] );
$parsed_args['ignore_sticky_posts'] = true;
$parsed_args['no_found_rows'] = true;
$get_posts = new WP_Query();
return $get_posts->query( $parsed_args );
* Adds a meta field to the given post.
* Post meta data is called "Custom Fields" on the Administration Screen.
* @param int $post_id Post ID.
* @param string $meta_key Metadata name.
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
* @param bool $unique Optional. Whether the same key should not be added.
* @return int|false Meta ID on success, false on failure.
function add_post_meta( $post_id, $meta_key, $meta_value, $unique = false ) {
// Make sure meta is added to the post, not a revision.
$the_post = wp_is_post_revision( $post_id );
return add_metadata( 'post', $post_id, $meta_key, $meta_value, $unique );
* Deletes a post meta field for the given post ID.
* You can match based on the key, or key and value. Removing based on key and
* value, will keep from removing duplicate metadata with the same key. It also
* allows removing all metadata matching the key, if needed.
* @param int $post_id Post ID.
* @param string $meta_key Metadata name.
* @param mixed $meta_value Optional. Metadata value. If provided,
* rows will only be removed that match the value.
* Must be serializable if non-scalar. Default empty.
* @return bool True on success, false on failure.
function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) {
// Make sure meta is deleted from the post, not from a revision.
$the_post = wp_is_post_revision( $post_id );
return delete_metadata( 'post', $post_id, $meta_key, $meta_value );
* Retrieves a post meta field for the given post ID.
* @param int $post_id Post ID.
* @param string $key Optional. The meta key to retrieve. By default,
* returns data for all keys. Default empty.
* @param bool $single Optional. Whether to return a single value.
* This parameter has no effect if `$key` is not specified.
* @return mixed An array of values if `$single` is false.
* The value of the meta field if `$single` is true.
* False for an invalid `$post_id` (non-numeric, zero, or negative value).
* An empty string if a valid but non-existing post ID is passed.
function get_post_meta( $post_id, $key = '', $single = false ) {
return get_metadata( 'post', $post_id, $key, $single );
* Updates a post meta field based on the given post ID.
* Use the `$prev_value` parameter to differentiate between meta fields with the
* If the meta field for the post does not exist, it will be added and its ID returned.
* Can be used in place of add_post_meta().
* @param int $post_id Post ID.
* @param string $meta_key Metadata key.
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
* @param mixed $prev_value Optional. Previous value to check before updating.
* If specified, only update existing metadata entries with
* this value. Otherwise, update all entries. Default empty.
* @return int|bool Meta ID if the key didn't exist, true on successful update,
* false on failure or if the value passed to the function
* is the same as the one that is already in the database.
function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) {
// Make sure meta is updated for the post, not for a revision.
$the_post = wp_is_post_revision( $post_id );
return update_metadata( 'post', $post_id, $meta_key, $meta_value, $prev_value );
* Deletes everything from post meta matching the given meta key.
* @param string $post_meta_key Key to search for when deleting.
* @return bool Whether the post meta key was deleted from the database.
function delete_post_meta_by_key( $post_meta_key ) {
return delete_metadata( 'post', null, $post_meta_key, '', true );
* Registers a meta key for posts.
* @param string $post_type Post type to register a meta key for. Pass an empty string
* to register the meta key across all existing post types.
* @param string $meta_key The meta key to register.
* @param array $args Data used to describe the meta key when registered. See
* {@see register_meta()} for a list of supported arguments.
* @return bool True if the meta key was successfully registered, false if not.
function register_post_meta( $post_type, $meta_key, array $args ) {
$args['object_subtype'] = $post_type;
return register_meta( 'post', $meta_key, $args );
* Unregisters a meta key for posts.
* @param string $post_type Post type the meta key is currently registered for. Pass
* an empty string if the meta key is registered across all
* @param string $meta_key The meta key to unregister.
* @return bool True on success, false if the meta key was not previously registered.
function unregister_post_meta( $post_type, $meta_key ) {
return unregister_meta_key( 'post', $meta_key, $post_type );
* Retrieves post meta fields, based on post ID.
* The post meta fields are retrieved from the cache where possible,
* so the function is optimized to be called more than once.
* @param int $post_id 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, zero, or negative value).
* An empty string if a valid but non-existing post ID is passed.
function get_post_custom( $post_id = 0 ) {
$post_id = absint( $post_id );
return get_post_meta( $post_id );
* Retrieves meta field names for a post.
* If there are no meta fields, then nothing (null) will be returned.
* @param int $post_id Optional. Post ID. Default is the ID of the global `$post`.
* @return array|void Array of the keys, if retrieved.
function get_post_custom_keys( $post_id = 0 ) {
$custom = get_post_custom( $post_id );
if ( ! is_array( $custom ) ) {
$keys = array_keys( $custom );
* Retrieves values for a custom post field.
* The parameters must not be considered optional. All of the post meta fields
* will be retrieved and only the meta field key values returned.
* @param string $key Optional. Meta field key. Default empty.
* @param int $post_id Optional. Post ID. Default is the ID of the global `$post`.
* @return array|null Meta field values.
function get_post_custom_values( $key = '', $post_id = 0 ) {
$custom = get_post_custom( $post_id );
return isset( $custom[ $key ] ) ? $custom[ $key ] : null;
* Determines whether a post is sticky.
* Sticky posts should remain at the top of The Loop. If the post ID is not
* given, then The Loop ID for the current post will be used.
* For more information on this and similar theme functions, check out
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
* Conditional Tags} article in the Theme Developer Handbook.
* @param int $post_id Optional. Post ID. Default is the ID of the global `$post`.
* @return bool Whether post is sticky.
function is_sticky( $post_id = 0 ) {
$post_id = absint( $post_id );
$stickies = get_option( 'sticky_posts' );
if ( is_array( $stickies ) ) {
$stickies = array_map( 'intval', $stickies );
$is_sticky = in_array( $post_id, $stickies, true );
* Filters whether a post is sticky.
* @param bool $is_sticky Whether a post is sticky.
* @param int $post_id Post ID.
return apply_filters( 'is_sticky', $is_sticky, $post_id );
* Sanitizes every post field.
* If the context is 'raw', then the post object or array will get minimal
* sanitization of the integer fields.
* @see sanitize_post_field()
* @param object|WP_Post|array $post The post object or array
* @param string $context Optional. How to sanitize post fields.
* Accepts 'raw', 'edit', 'db', 'display',
* 'attribute', or 'js'. Default 'display'.
* @return object|WP_Post|array The now sanitized post object or array (will be the
function sanitize_post( $post, $context = 'display' ) {
if ( is_object( $post ) ) {
// Check if post already filtered for this context.
if ( isset( $post->filter ) && $context == $post->filter ) {
if ( ! isset( $post->ID ) ) {
foreach ( array_keys( get_object_vars( $post ) ) as $field ) {
$post->$field = sanitize_post_field( $field, $post->$field, $post->ID, $context );
$post->filter = $context;
} elseif ( is_array( $post ) ) {
// Check if post already filtered for this context.
if ( isset( $post['filter'] ) && $context == $post['filter'] ) {
if ( ! isset( $post['ID'] ) ) {
foreach ( array_keys( $post ) as $field ) {
$post[ $field ] = sanitize_post_field( $field, $post[ $field ], $post['ID'], $context );
$post['filter'] = $context;
* Sanitizes a post field based on context.
* Possible context values are: 'raw', 'edit', 'db', 'display', 'attribute' and
* 'js'. The 'display' context is used by default. 'attribute' and 'js' contexts
* are treated like 'display' when calling filters.
* @since 4.4.0 Like `sanitize_post()`, `$context` defaults to 'display'.
* @param string $field The Post Object field name.
* @param mixed $value The Post Object value.
* @param int $post_id Post ID.
* @param string $context Optional. How to sanitize the field. Possible values are 'raw', 'edit',
* 'db', 'display', 'attribute' and 'js'. Default 'display'.
* @return mixed Sanitized value.
function sanitize_post_field( $field, $value, $post_id, $context = 'display' ) {
$int_fields = array( 'ID', 'post_parent', 'menu_order' );
if ( in_array( $field, $int_fields, true ) ) {
// Fields which contain arrays of integers.
$array_int_fields = array( 'ancestors' );
if ( in_array( $field, $array_int_fields, true ) ) {
$value = array_map( 'absint', $value );
if ( 'raw' === $context ) {
if ( str_contains( $field, 'post_' ) ) {
$field_no_prefix = str_replace( 'post_', '', $field );
if ( 'edit' === $context ) {
$format_to_edit = array( 'post_content', 'post_excerpt', 'post_title', 'post_password' );
* Filters the value of a specific post field to edit.
* The dynamic portion of the hook name, `$field`, refers to the post
* @param mixed $value Value of the post field.
* @param int $post_id Post ID.
$value = apply_filters( "edit_{$field}", $value, $post_id );
* Filters the value of a specific post field to edit.
* The dynamic portion of the hook name, `$field_no_prefix`, refers to
* @param mixed $value Value of the post field.
* @param int $post_id Post ID.
$value = apply_filters( "{$field_no_prefix}_edit_pre", $value, $post_id );
$value = apply_filters( "edit_post_{$field}", $value, $post_id );
if ( in_array( $field, $format_to_edit, true ) ) {
if ( 'post_content' === $field ) {
$value = format_to_edit( $value, user_can_richedit() );
$value = format_to_edit( $value );
$value = esc_attr( $value );
} elseif ( 'db' === $context ) {
* Filters the value of a specific post field before saving.
* The dynamic portion of the hook name, `$field`, refers to the post
* @param mixed $value Value of the post field.
$value = apply_filters( "pre_{$field}", $value );
* Filters the value of a specific field before saving.
* The dynamic portion of the hook name, `$field_no_prefix`, refers
* to the post field name.
* @param mixed $value Value of the post field.
$value = apply_filters( "{$field_no_prefix}_save_pre", $value );
$value = apply_filters( "pre_post_{$field}", $value );
* Filters the value of a specific post field before saving.
* The dynamic portion of the hook name, `$field`, refers to the post
* @param mixed $value Value of the post field.
$value = apply_filters( "{$field}_pre", $value );
// Use display filters by default.
* Filters the value of a specific post field for display.
* The dynamic portion of the hook name, `$field`, refers to the post
* @param mixed $value Value of the prefixed post field.
* @param int $post_id Post ID.
* @param string $context Context for how to sanitize the field.
* Accepts 'raw', 'edit', 'db', 'display',
* 'attribute', or 'js'. Default 'display'.
$value = apply_filters( "{$field}", $value, $post_id, $context );
$value = apply_filters( "post_{$field}", $value, $post_id, $context );
if ( 'attribute' === $context ) {
$value = esc_attr( $value );
} elseif ( 'js' === $context ) {
$value = esc_js( $value );