Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active October 6, 2015 11:59
Show Gist options
  • Save kurozumi/42d10f8eb8dfb99944df to your computer and use it in GitHub Desktop.
Save kurozumi/42d10f8eb8dfb99944df to your computer and use it in GitHub Desktop.
【ワードプレス】指定した投稿タイプのカスタムフィールドのキーをすべて取得する関数
<?php
if(!function_exists('get_meta_keys_from'):
function get_meta_keys_from($post_type)
{
global $wpdb;
$query = <<< __SQL__
SELECT
meta_key
FROM $wpdb->postmeta
JOIN $wpdb->posts
ON $wpdb->posts.ID = $wpdb->postmeta.post_id
WHERE
$wpdb->posts.post_type LIKE '%s' AND
$wpdb->postmeta.meta_key NOT LIKE '%s'
GROUP BY meta_key
ORDER BY meta_key
__SQL__;
$prepare = $wpdb->prepare($query, array($post_type, '\_%'));
return $wpdb->get_col($prepare);
}
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment