【发布时间】:2017-01-11 18:34:06
【问题描述】:
我编写这些函数是为了根据元数据过滤帖子。上下文是一个房地产网站,其属性是 CPT。在这些功能中,我通过销售它们的代理在管理端过滤属性。该功能适用于任何新尝试。如果选择了代理,过滤器将仅显示该代理出售的房产。
在初始过滤后,我遇到的问题仍然存在。代理列表就消失了。我感觉我为创建列表而运行的循环由于某种原因而被停止。
为了直观地说明,这里是过滤器列表在搜索之前的样子。
这是使用过滤器后的样子
显然,如果 ADMIN_FILTER_FIELD_VALUE=(id number here) 出现在带有任何设置值的 url 中,则循环不会运行。
这是所有这些的代码。
add_filter( 'parse_query', 'agents_posts_filter' );
function agents_posts_filter( $query ){
global $pagenow;
$type = 'post';
if (isset($_GET['post_type'])) {
$type = $_GET['post_type'];
}
if ( 'properties' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '') {
$query->query_vars['meta_key'] = 'select-agent-value';
$query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE'];
}
}
add_action('restrict_manage_posts', 'filter_post_type_by_agent');
function filter_post_type_by_agent(){
global $pagenow;
$type = 'post';
if (isset($_GET['post_type'])) {
$type = $_GET['post_type'];
}
if ('properties' == $type && is_admin() && $pagenow=='edit.php') {
?>
<select name="ADMIN_FILTER_FIELD_VALUE">
<option value=""><?php _e('Filter By Agent'); ?></option>
<?php
$args = array(
'post_type' => 'agents',
'posts_per_page' => -1
);
$posts = new WP_Query($args);
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
<option value="<?php the_ID(); ?>"> <?php the_title(); ?> </option>
<?php
endwhile;
endif;
?>
</select>
<?php
}
}
我在这里有什么明显的遗漏吗?感谢任何人都可以提供的任何帮助。
【问题讨论】:
标签: php wordpress filtering admin custom-fields