Skip to content

Instantly share code, notes, and snippets.

@actual-saurabh
Last active July 2, 2021 10:35
Show Gist options
  • Save actual-saurabh/48d7fb34ad1de2e912e651890ae29690 to your computer and use it in GitHub Desktop.
Save actual-saurabh/48d7fb34ad1de2e912e651890ae29690 to your computer and use it in GitHub Desktop.
Final code for Metadata API
<?php
/**
* Installs table for badgemeta
*
*/
function badge_meta_install() {
global $wpdb;
$table_name = $wpdb->prefix . 'badgemeta';
$charset_collate = $wpdb->get_charset_collate();
// see wpdb_get_schema() in https://github.com/WordPress/WordPress/blob/master/wp-admin/includes/schema.php
$max_index_length = 191;
$install_query = "CREATE TABLE $table_name (
meta_id bigint(20) unsigned NOT NULL auto_increment,
badge_id bigint(20) unsigned NOT NULL default '0',
meta_key varchar(255) default NULL,
meta_value longtext,
PRIMARY KEY (meta_id),
KEY badge (badge_id),
KEY meta_key (meta_key($max_index_length))
) $charset_collate;"
dbDelta( $install_query );
}
<?php
// hook into init for single site, priority 0 = highest priority
add_action('init', 'badgemeta_integrate_wpdb', 0);
// hook in to switch blog to support multisite
add_action( 'switch_blog', 'badgemeta_integrate_wpdb', 0 );
/**
* Integrates badgemeta table with $wpdb
*
*/
function badgemeta_integrate_wpdb() {
global $wpdb;
$wpdb->badgemeta = $wpdb->prefix . 'badgemeta';
$wpdb->tables[] = 'badgemeta';
return;
}
<?php
/**
* Adds meta data field to a badge.
*
* @param int $badge_id Badge ID.
* @param string $meta_key Metadata name.
* @param mixed $meta_value Metadata value.
* @param bool $unique Optional, default is false. Whether the same key should not be added.
* @return int|false Meta ID on success, false on failure.
*/
function add_badge_meta($badge_id, $meta_key, $meta_value, $unique = false) {
return add_metadata('badge', $badge_id, $meta_key, $meta_value, $unique);
}
/**
* Removes metadata matching criteria from a badge.
*
* 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 key, if needed.
*
* @param int $badge_id Badge ID
* @param string $meta_key Metadata name.
* @param mixed $meta_value Optional. Metadata value.
* @return bool True on success, false on failure.
*/
function delete_badge_meta($badge_id, $meta_key, $meta_value = '') {
return delete_metadata('badge', $badge_id, $meta_key, $meta_value);
}
/**
* Retrieve meta field for a badge.
*
* @param int $badge_id Badge 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_badge_meta($badge_id, $key = '', $single = false) {
return get_metadata('badge', $badge_id, $key, $single);
}
/**
* Update badge meta field based on badge ID.
*
* Use the $prev_value parameter to differentiate between meta fields with the
* same key and badge ID.
*
* If the meta field for the user does not exist, it will be added.
*
* @param int $badge_id Badge ID.
* @param string $meta_key Metadata key.
* @param mixed $meta_value Metadata value.
* @param mixed $prev_value Optional. Previous value to check before removing.
* @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
*/
function update_badge_meta($badge_id, $meta_key, $meta_value, $prev_value = '') {
return update_metadata('badge', $badge_id, $meta_key, $meta_value, $prev_value);
}
@biswajitpaul01
Copy link

How can I search meta_value using wp_query or wp_meta_query?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment