Created
October 16, 2018 06:48
-
-
Save man4toman/3657074670aa400c85834b9464c21341 to your computer and use it in GitHub Desktop.
Determine if a meta key is set for a given object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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