【问题标题】:Meta Query in Wordpress is being ignoredWordpress 中的元查询被忽略
【发布时间】: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] =&gt; -1 [post_type] =&gt; employees [meta_query] =&gt; Array ( [key] =&gt; department [value] =&gt; Sales, Marketing &amp; Revenue [compare] =&gt; = ) )

然而,销售营销和收入只有 1 个人,而不是 3 人(我到目前为止输入的员工总数)。看起来它忽略了元查询。

有人可以帮我吗?

【问题讨论】:

标签: php wordpress


【解决方案1】:

我在数据库中查找了它是如何输入的。我意识到我不久前用正确的字体更新了文件。 Wordpress 没有更新数据库中的这些更改,因此我不得不调用旧的键名,而不是新的键名。我想这是我这边的一个缺陷,为什么它无法读取它。现在正确的查询如下。

$args = array(
    'post_type' => 'employees',
    'meta_query' => array(
        array(
            'key'     => 'shm_text',
            'value' => $page,
            'compare' => '='
        )
    )
);

【讨论】:

    猜你喜欢
    • 2020-05-26
    • 1970-01-01
    • 2013-04-13
    • 2013-09-04
    • 2014-11-20
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多