我有自定义的帖子类型"soto_property“。我做了一个自定义过滤器,根据使用过的名为"operations“的元数据过滤post列表。这是我的代码-
<?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=operation和meta_value=1或meta_value=2存储在DB中的wp_postmeta表中。
有人能帮我吗。
发布于 2015-04-24 05:05:01
你的代码有很多地方都有问题:
$_GET、$_POST或$_REQUEST)获得的值,除非对它们进行清理。您应该阅读以下内容:函数soto_posts_filter_restrict_manage_posts中的Validating Sanitizing and Escaping User Data.<select>代码建议只能使用一个值,所以为什么要使用DISTINCT呢?另外,该语句不应该链接到帖子ID吗?这就是为什么你没有得到你所期望的结果的原因。我建议你在这里和那里添加一些var_dump,并验证流程中的每个步骤,以确保您一直都有预期的结果。
https://stackoverflow.com/questions/29833670
复制相似问题