环顾四周,无法找到解决方案。所以我的问题是:
我有一个自定义字段(复选框),例如:my_custom_field_checkbox
。我有5个帖子已选中此复选框。我需要以某种方式在前端使用它,例如:
管理
[复选框]我读过这本书
前端:
阅读该书的人( 5 )
那么,我怎么能这样做?谢谢!
好的,我正在检查这个问题和谷歌搜索,我想出了那个代码,它有效,它允许我计算相同自定义字段的不同值:
$rbBuildCatsArray = array('cat_top_managers_args'=>__('Top management, managers','ibchrrb'),'cat_car_business_args'=>__('Car Business','ibchrrb'),'administrative_staff'=>__('Administrative staff','ibchrrb'),'banks_insurance_leasing'=>__('Banks, insurance, leasing','ibchrrb'),'safety_guard'=>__('Safety, guard','ibchrrb'),'accounting_finance_enterprise_economy'=>__('Accounting, finance, enterprise economy','ibchrrb'),'public_service_nonprofit_organizations'=>__('Public service, non-profit organizations','ibchrrb'),'design_art_entertainment'=>__('Design, art, entertainment','ibchrrb'),'home_staff_service'=>__('Home staff, service','ibchrrb'),'procurement_supply_fea'=>__('Procurement, supply, FEA','ibchrrb'),'internet_it_telecom_communication'=>__('Internet, IT, telecom, communication','ibchrrb'),'logistics_transport_storage'=>__('Logistics, transport, storage','ibchrrb'),'marketing_advertising_pr'=>__('Marketing, Advertising, PR','ibchrrb'),'healthcare_jobs'=>__('Healthcare Jobs','ibchrrb'),'science_education_consulting'=>__('Science, education, consulting','ibchrrb'),'hr_department_hr_training'=>__('HR department, HR, training','ibchrrb'),'part_time_seasonal_work'=>__('Part-time, seasonal work','ibchrrb'),'sales'=>__('Sales','ibchrrb'),'industry_agriculture'=>__('Industry, agriculture','ibchrrb'),'jobs_for_youth'=>__('Jobs for Youth','ibchrrb'),'workmen_laborers'=>__('Workmen, laborers','ibchrrb'),'restaurateurs_chefs_waiters'=>__('Restaurateurs, chefs, waiters','ibchrrb'),'retail_trade'=>__('Retail, trade','ibchrrb'),'media_publishing_printing'=>__('Media, publishing, printing','ibchrrb'),'sports_fitness_beauty_salons'=>__('Sports, fitness, beauty salons','ibchrrb'),'real_estate'=>__('Real estate','ibchrrb'),'tourism_hotels'=>__('Tourism, hotels','ibchrrb'),'services_repair_and_maintenance'=>__('Services, repair and maintenance','ibchrrb'),'jurisprudence'=>__('Jurisprudence','ibchrrb'));
foreach($rbBuildCatsArray as $category=>$label){
$unqCatVariable = '$cat_'.$category.'_args';
$unqCatVariableCount = '$cat_'.$category.'_count';
$unqCatVariable = array('post_type' => 'paibcresume','posts_per_page' => -1,'meta_query'=>array(),'meta_key' => '');
$unqCatVariable['meta_query'][] = array('key'=>'rbwwcategoryhidden','value'=>$label,'compare'=>'LIKE');
$unqCatVariableCount = new WP_Query($unqCatVariable);
echo '<div>' . $label . ' ' . $unqCatVariableCount->post_count . '</div>';
}
下面是我所谈论的截图。我的自定义帖子类型中有自定义字段。我得到该自定义字段的值并将它们计算为(粉色区域)。
答案 0 :(得分:1)
你可以同时使用内置函数和直接sql。
query_posts('meta_key=my-key-name&meta_value=my-meta-value');
echo $wp_query->found_posts;
或使用sql查询
$sql = "SELECT count(DISTINCT pmeta.post_id)
FROM $wpdb->postmeta pmeta
JOIN $wpdb->posts posts ON (posts.ID = pmeta.post_id)
WHERE pmeta.meta_key = 'my-meta-key'
AND pmeta.meta_value = 'my-meta-value'
AND posts.post_type = 'post'
AND posts.post_status = 'publish' ";
$count = $wpdb->get_var($sql);
echo $count;