重拨此项。现在,它应该适用于任何和所有post\\u类型:
<?php
add_action(\'init\', \'setup_events_rewrite\');
//Gives you a new query var and sets up a rewrite rule that makes events/ work
function setup_events_rewrite() {
//create a query var
add_rewrite_tag(\'%tax_archive%\',\'([^/]+)\');
//set new query vart with tax of choiice
add_rewrite_rule( \'events/?$\', \'index.php?tax_archive=events\', \'top\' );
}
add_filter(\'request\', \'setup_events_request\');
//sets the post_type is tax_archive is set to events
function setup_events_request($qv) {
if(isset($qv[\'tax_archive\']) && $qv[\'tax_archive\'] == \'events\') {
$qv[\'post_type\'] = \'any\'; //set post type to whatever you like here
}
return $qv;
}
add_filter( \'posts_clauses\', \'setup_events_post_clause\', 10, 2 );
//If the query var tax_archive is set and is equal to events, it\'ll change the query to pull only posts that have a term from the events taxonomy
function setup_events_post_clause($clauses, $wp_query) {
global $wpdb;
if(isset($wp_query->query[\'tax_archive\']) && \'events\' == $wp_query->query[\'tax_archive\']) {
$clauses[\'join\'].= <<<SQL
LEFT OUTER JOIN {$wpdb->term_relationships} ON {$wpdb->posts}.ID={$wpdb->term_relationships}.object_id
LEFT OUTER JOIN {$wpdb->term_taxonomy} USING (term_taxonomy_id)
LEFT OUTER JOIN {$wpdb->terms} USING (term_id)
SQL;
$clauses[\'where\'] .= $wpdb->prepare(" AND taxonomy = %s", $wp_query->query[\'tax_archive\']);
$clauses[\'groupby\'] = "object_id";
}
return $clauses;
}
?>