Skip to content

Instantly share code, notes, and snippets.

@maheshwaghmare
Created July 6, 2019 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maheshwaghmare/84fa96b952eadfd56d25c5d2852cd70f to your computer and use it in GitHub Desktop.
Save maheshwaghmare/84fa96b952eadfd56d25c5d2852cd70f to your computer and use it in GitHub Desktop.
Get all post meta keys from the WordPress with SQL query.
<?php
if( ! function_exists( 'prefix_get_all_post_meta_keys' ) ) :
/**
* Get all meta keys.
*
* @return mixed Array of the meta keys.
*/
function prefix_get_all_post_meta_keys() {
global $wpdb;
$sql = "SELECT DISTINCT meta_key
FROM $wpdb->postmeta
WHERE meta_key NOT BETWEEN '_' AND '_z'
HAVING meta_key NOT LIKE %s
ORDER BY meta_key";
$keys = $wpdb->get_col( $wpdb->prepare( $sql, $wpdb->esc_like( '_' ) . '%' ) );
return $keys;
}
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment