我在admin cpt posts视图中添加了一个自定义下拉过滤器。
我正在使用以下功能连接“restrict\\u manage\\u posts”操作:
add_action(\'restrict_manage_posts\', \'show_filters\' );
function show_filters() {
$tag = isset( $_GET[ \'partner\' ] ) ? esc_attr( $_GET[ \'partner\' ] ) : null;
$args = array(
\'post_type\' => \'partner\',
);
$partners_query = new WP_Query( $args );
$partners_options = array( \'\' => \'Choose partner\' );
if ( ! empty( $partners_query->posts ) ) {
foreach ( $partners_query->posts as $partner ) {
$partners_options[$partner->ID] = $partner->post_title;
}
} else {
// Just for check that the query is returning empty
$partners_options = array( \'\' => \'EMPTY QUERY: Debugging purposes\' );
}
if ( ! empty( $partners_options ) ) {
echo \'<select name="partner" class="postform">\';
foreach ( $partners_options as $id => $name ) {
if( $name ) {
printf(\'<option value="%s"%s>%s</option>\', esc_attr( $id ), selected( $id, $tag, false), esc_html( $name ) );
}
}
echo \'</select>\';
}
}
EDIT: 我有一个pre\\u get\\u posts过滤器,它将meta\\u查询添加到过滤中:
add_action( \'pre_get_posts\', \'orderby_filtering\' );
function orderby_filtering( $query ) {
if ( isset( $_GET[ \'partner\' ] ) && $_GET[ \'partner\' ] ) {
$query->query_vars[\'meta_query\'][] = array(
\'key\' => \'partner_select\',
\'value\' => (int)$_GET[\' partner \']
);
}
}
过滤器工作正常,返回正确的值,但一旦执行,新的下拉列表将显示“空查询”消息,而不是应该显示的合作伙伴列表。
为什么不再执行查询?我错过了什么?谢谢