0
我写了这些功能,以过滤基于元数据的帖子。上下文是一个房地产网站,属性是CPT。在这些功能中,我通过销售代理的方式过滤管理端的属性。该功能适用于任何新的尝试。如果选择了座席,则该过滤器将仅显示该座席销售的属性。Wordpress管理按元过滤,搜索后过滤器选项消失
我在初始过滤后仍然存在问题。代理列表简单消失。我感觉到我创建列表的循环出于某种原因被停止。
为了清楚地说明,下面是过滤器列表在搜索之前的外观。
这里是它的外观使用过滤器
显然,如果ADMIN_FILTER_FIELD_VALUE =(这里ID号)出现在任何一组值的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
}
}
有什么明显的我在这里失踪?感谢任何人都可以提供的帮助。
不知道这个解决方案。我会让你知道,如果我得到它的工作:O –
使用get_posts从一个完全不同的自定义帖子类型,而不是代理cpt:/ –
感谢您的帮助! :D添加is_main_query()是为了防止列表不发布。我从来不会猜测问题来自agent_posts_filter函数。 –