Skip to content

Instantly share code, notes, and snippets.

@man4toman
Created October 16, 2018 06:48
Show Gist options
  • Save man4toman/3657074670aa400c85834b9464c21341 to your computer and use it in GitHub Desktop.
Save man4toman/3657074670aa400c85834b9464c21341 to your computer and use it in GitHub Desktop.
Determine if a meta key is set for a given object
<?php
/**
* @link https://developer.wordpress.org/reference/functions/get_post_meta/
**/
// Only for post types
if( get_post_meta( $post_id, '_meta_key', true ) ) {
// do something
}
/**
* @link https://developer.wordpress.org/reference/functions/metadata_exists/
**/
// Check and get a term meta
if ( metadata_exists( 'term', $term_id, '_meta_key' ) ) {
// do something
}
// Check and get a post meta
if ( metadata_exists( 'post', $post_id, '_meta_key' ) ) {
// do something
}
// Check and get a user meta
if ( metadata_exists( 'user', $user_id, '_meta_key' ) ) {
// do something
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment