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函數。 –