我希望得到所有帖子的post_meta值。
对于数据库表wp_postmeta,我想得到key' _job_hourly_fee'的元值。对于所有帖子,不仅是单个帖子。不知道该怎么做。提前谢谢。
答案 0 :(得分:2)
这将为您提供该字段存在的所有值的数组。您可以对其进行调整以提供&post;标题:_job_hourly_fee值'。 只需将此代码放入您的主题(或儿童主题的)functions.php中,并将短代码[job_hourly_fee_values]添加到您想要显示结果的帖子或页面HTML内容中。
<?php
function get_fee_values(){
$jhf_values = array('');//AN EMPTY ARRAY TO STORE THE VALUES GATHERED
$the_query = new WP_Query( 'post_type=post' );//CHANGE TO CUSTOM POST TYPE IF REQUIRED
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$the_answer=get_post_meta($the_query->ID, '_job_hourly_fee' , true);//'TRUE' WILL RETURN ONLY ONE VALUE FOR EACH POST.
$the_answer=trim($the_answer);//REMOVE UNWANTED WHITESPACE FROM BEGINNING AND END
array_push($jhf_values, $the_answer);//ADD THE RESULT TO THE EMPTY ARRAY
}
}
$jhf_values = array_unique($jhf_values);//NOW WEVE GOT ALL THE VALUES, WE CAN REMOVE DUPLICATES
$jhf_values = sort($jhf_values);//AND WE CAN PUT THEM IN ORDER
//NOW WE CAN RUN THROUGH THE ARRAY AND DO SOMETHING WITH EACH OBJECT...
foreach ($jhf_values as $value) {
echo "<p>".$value."</p>";
}
}
?>
<?php
add_shortcode('job_hourly_fee_values', 'get_fee_values');?> //PLACE SHORTCODE [job_hourly_fee_values] IN YOUR POST OR PAGE HTML CONTENT WHERE YOU WANT THE RESULT TO APPEAR
?>