好了,伙计们,我又用另一种方式做了。。我是这样做的:
add_filter( \'pre_get_posts\', function( $query ) {
if( ! is_main_query() || ! is_post_type_archive() || ! $query->get( \'section\', false ) )
return $query;
global $wpdb;
$section = $query->get( \'section\' );
unset( $query->query[\'section\'] );
unset( $query->query_vars[\'section\'] );
$query->tax_query = false;
$query->set( \'tax_query\', false );
$cateroties = $wpdb->get_results( $wpdb->prepare(
"
SELECT option_name, option_value
FROM $wpdb->options
WHERE option_name LIKE \'%s\'
",
\'category_%_section\'
) );
$category__in = array();
foreach( $cateroties as $category ) {
$cat_id = $category->option_name;
$values = unserialize( $category->option_value );
if( in_array( $section, $values ) ) {
$category__in[] = str_replace( \'category_\', \'\', str_replace( \'_section\', \'\', $cat_id ) );
}
//
}
$query->set( \'category__in\', $category__in );
return $query;
} );