最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - How to get meta key list efficiently?

programmeradmin0浏览0评论

I have this function to get the meta keys for all the post types.

 public function pgggo_custom_post_type_keylist()
        {
            //gets the list of post types 
            $pgggo_custompostype_array = $this->pgggo_list_of_posttypes();

            $pgggo_getposts            = array();

            foreach ($pgggo_custompostype_array as $value) {
                $args = array(
                    'numberposts' => 1,
                    'post_type'   => $value,
                );
                if (!empty(get_posts($args)[0]->ID)) {
                    $pgggo_getposts[] = get_posts($args)[0]->ID;
                }

            }
            $new_array = array();
            foreach ($pgggo_getposts as $value) {
                $new_array[] = get_post_custom($value);
            }

            $result = array();
            foreach ($new_array as $sub) {
                $result = array_merge($result, $sub);
            }
            $result     = array_keys($result);
            $result     = array_unique($result);
            $result     = array_combine($result, $result);
            $result[''] = 'NONE';
            return $result;
        }

Though it works fine it results in high database queries and duplicates queries. Is there any method to improve this or alternative solutions?

I have this function to get the meta keys for all the post types.

 public function pgggo_custom_post_type_keylist()
        {
            //gets the list of post types 
            $pgggo_custompostype_array = $this->pgggo_list_of_posttypes();

            $pgggo_getposts            = array();

            foreach ($pgggo_custompostype_array as $value) {
                $args = array(
                    'numberposts' => 1,
                    'post_type'   => $value,
                );
                if (!empty(get_posts($args)[0]->ID)) {
                    $pgggo_getposts[] = get_posts($args)[0]->ID;
                }

            }
            $new_array = array();
            foreach ($pgggo_getposts as $value) {
                $new_array[] = get_post_custom($value);
            }

            $result = array();
            foreach ($new_array as $sub) {
                $result = array_merge($result, $sub);
            }
            $result     = array_keys($result);
            $result     = array_unique($result);
            $result     = array_combine($result, $result);
            $result[''] = 'NONE';
            return $result;
        }

Though it works fine it results in high database queries and duplicates queries. Is there any method to improve this or alternative solutions?

Share Improve this question asked Sep 10, 2019 at 18:57 user145078user145078 1
  • 1 Can't you just query the postmeta table directly and use DISTINCT in the in the SQL? It might help if you explain why you are doing this. – Ted Stresen-Reuter Commented Sep 10, 2019 at 21:55
Add a comment  | 

1 Answer 1

Reset to default 0

As mentioned in the comments you can query the postmeta table directly:

public function get_metadata_keys(){
    global $wpdb;

    $meta_query = $wpdb->get_results(
        "
        SELECT DISTINCT meta_key
        FROM {$wpdb->postmeta}
        "
    , ARRAY_A );

    $meta_keys = wp_list_pluck( $meta_query, 'meta_key' );
    return $meta_keys;
}

This will return the list of all meta_keys associated to any post.

发布评论

评论列表(0)

  1. 暂无评论