WordPress select query issue 时间:2018-02-05 作者:Sanjay Yadav 正在尝试从中提取所有数据wp_postmeta 桌子我想先去拿meta_key 列值。因此,我正在运行此查询。当我使用print_r 然后显示所有数据,但当我使用foreach循环时,它不工作。<?php global $wpdb; $myrows = $wpdb->get_results( "SELECT * FROM wp_postmeta" ); foreach($myrows as $value){ echo $value->sleeps; } 3 个回复 最合适的回答,由SO网友:scytale 整理而成 锅里还有一个:$wpdb->get_col 可用于返回为“睡眠”设置的所有值的“一维”数组global $wpdb; $values = $wpdb->get_col( "SELECT meta_value FROM wp_postmeta WHERE meta_key = \'sleeps\'" ); echo implode(\',\', array_unique($values) ); SO网友:Sanjay Yadav 好朋友,这个问题解决了我的问题。global $wpdb; $query = $wpdb->get_results("SELECT DISTINCT meta_value FROM wp_postmeta WHERE meta_key = \'sleeps\' ORDER BY meta_value"); if (count($query)) { foreach ($query as $state) { echo $state->meta_value ; } } SO网友:Max Yudin <?php global $wpdb; $myrows = $wpdb->get_results( "SELECT * FROM wp_postmeta" ); foreach($myrows as $row){ echo $row->meta_key; // meta key } 或者,您可以获取post元密钥:$fields = get_post_custom_keys(); foreach ( $fields as $key => $value ) { echo $key; } 结束 文章导航