【发布时间】:2016-01-18 10:55:27
【问题描述】:
我编写了一个名为“员工”的自定义帖子类型,现在尝试通过在显示部门页面后调用此帖子类型来查询它。我创建了一个自定义元框,其中包含几个参数,例如部门,带有下拉菜单,因此人们可以轻松地将人员分配到正确的部门。如果满足条件,则应该显示员工的照片(或者在我的情况下,crap 作为测试字样)。
但是,我似乎在尝试获取正确的查询时遇到了问题。我的代码如下。我仍然需要清除一些东西,并且它的代码正在进行中,所以不要击落它是如何编程的。它只是现在的查询。
这是元框的代码
$prefix = 'shm_';
$meta_box = array(
'id' => 'meta-employees',
'title' => 'Overige informatie',
'page' => 'employees',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'position',
'desc' => 'The name of the job position',
'id' => $prefix . 'position',
'type' => 'text',
'std' => ''
),
array(
'name' => 'department',
'desc' => 'Select the department this employee is working in',
'id' => $prefix . 'department',
'type' => 'select',
'options' => array('Board', 'Sales, Marketing & Revenue', 'Option 3', 'Option 4', 'Option 5', 'Option 6')
)
)
);
这是用于查询的
<?php
if(!is_front_page()){
if (is_page('board')){
$page = 'board';
}elseif (is_page('sales-marketing-revenue')){
$page = 'Sales, Marketing & Revenue';
}
echo $page;
$i = 0;
$args = array(
'numberposts' => -1,
'post_type' => 'employees',
'meta_query' => array(
'key' => 'department',
'value' => $page,
'compare' => '=',
)
);
query_posts($args);
if ( have_posts() ) :
while ( have_posts() ) : the_post();
echo "crap";
?>
<?php endwhile; ?>
<?php endif; ?>
<?php } ?>
这件作品在技术上是可行的,但它会吐出所有员工,而不是我在特定部门设置的员工,例如 board。当我打印数组时,一切似乎都正常。
Array ( [numberposts] => -1 [post_type] => employees [meta_query] => Array ( [key] => department [value] => Sales, Marketing & Revenue [compare] => = ) )
然而,销售营销和收入只有 1 个人,而不是 3 人(我到目前为止输入的员工总数)。看起来它忽略了元查询。
有人可以帮我吗?
【问题讨论】:
-
准确地说是在 Wordpress 的法典中:codex.wordpress.org/Class_Reference/WP_Meta_Query