我想获取所有帖子的 post_meta 值。
对于数据库表 wp_postmeta,我想获取所有帖子的键“_job_hourly_fee”的元值,而不仅仅是单个帖子。不知道该怎么办。提前致谢。
这将为您提供该字段存在的所有值的数组。您可以对其进行调整以提供“post_title:_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
?>