首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义过滤器函数不适用于自定义帖子类型

自定义过滤器函数不适用于自定义帖子类型
EN

Stack Overflow用户
提问于 2015-04-24 04:27:31
回答 1查看 545关注 0票数 2

我有自定义的帖子类型"soto_property“。我做了一个自定义过滤器,根据使用过的名为"operations“的元数据过滤post列表。这是我的代码-

代码语言:javascript
复制
<?php
add_filter( 'parse_query', 'soto_posts_filter' );
add_action( 'restrict_manage_posts', 'soto_posts_filter_restrict_manage_posts' );

function soto_posts_filter( $query )
{
    global $pagenow;
    if( is_admin() AND $query->query['post_type'] == 'soto_property' ) {
        $qv = &$query->query_vars;
        $qv['meta_query'] = array();

        if( !empty( $_GET['operations'] ) ) {
          $qv['meta_query'][] = array(
            'field' => 'operations',
            'value' => $_GET['operations'],
            'compare' => 'LIKE',
          );
        }

    }
}

function soto_posts_filter_restrict_manage_posts()
{
    global $wpdb;
    if($_GET['post_type']=='soto_property')
    {
        $sql = 'SELECT DISTINCT meta_key FROM '.$wpdb->postmeta.' where meta_key="operations" ORDER BY 1';
        $fields = $wpdb->get_results($sql, ARRAY_N);

 ?>
<select name="operations" id="filter-operations" class="custom-filter" style="display:none; width: 15%;" >
        <option value=""></option>
        <option value="2" <?php echo $_GET['operations']==2?"selected='selected'":'' ?>>Rent</option>
        <option value="1" <?php echo $_GET['operations']==1?"selected='selected'":'' ?>>Sale</option>
      </select>
<?php
    }
}

但我的帖子并没有根据元数据“操作”进行过滤。此元数据使用meta_key=operationmeta_value=1meta_value=2存储在DB中的wp_postmeta表中。

有人能帮我吗。

EN

回答 1

Stack Overflow用户

发布于 2015-04-24 05:05:01

你的代码有很多地方都有问题:

  1. 您永远不应该使用从用户($_GET$_POST$_REQUEST)获得的值,除非对它们进行清理。您应该阅读以下内容:函数soto_posts_filter_restrict_manage_posts中的Validating Sanitizing and Escaping User Data.
  2. Your Select语句是无用的,因为您不会对它做任何操作。此外,您的<select>代码建议只能使用一个值,所以为什么要使用DISTINCT呢?另外,该语句不应该链接到帖子ID吗?

这就是为什么你没有得到你所期望的结果的原因。我建议你在这里和那里添加一些var_dump,并验证流程中的每个步骤,以确保您一直都有预期的结果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29833670

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档