我正在使用以下代码进行tax\\u查询,但它似乎不再起作用了。。
在某种程度上,它是有效的,对我来说,它将不再有效。
<?php
global $pf_override;
class PF_override {
public $site_terms = array();
function __construct() {
// add filters
add_filter( \'init\', array( &$this, \'populates_site_terms\' ) );
add_filter( \'pre_get_posts\', array( &$this, \'filter_query\' ) );
}
function populates_site_terms() {
// my actual function does some processing and
// checks caches and combines IDs from multiple
// different sources here and then sets site_terms
$this->site_terms = array( 1, 2, 3 );
}
function filter_query( $query ) {
// not the main query?
if ( !$query->is_main_query() ) {
return $query;
}
// have terms to filter by?
if ( !empty( $this->site_terms ) ) {
// construct tax_query
$tax_query = array(
\'taxonomy\' => \'site_category\',
\'field\' => \'id\',
\'terms\' => $this->site_terms,
\'operator\' => \'IN\'
);
// this needs to be an array of arrays
$taxquery = array(
$tax_query
);
// set this..
$query->set( \'tax_query\', $taxquery );
}
// return new query object
return $query;
}
}
// not the admin? init class
if ( !is_admin() ) {
$pf_override = new PF_override();
}
?>
分类添加如下:
function PF_register_post_type() {
$labels = array(
\'name\' => _x( \'Site Category\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Site Category\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Site Categories\' ),
\'all_items\' => __( \'All Site Categories\' ),
\'parent_item\' => __( \'Parent Site Category\' ),
\'parent_item_colon\' => __( \'Parent Site Category:\' ),
\'edit_item\' => __( \'Edit Site Category\' ),
\'update_item\' => __( \'Update Site Category\' ),
\'add_new_item\' => __( \'Add New Site Category\' ),
\'new_item_name\' => __( \'New Site Category Name\' ),
\'menu_name\' => __( \'Site Category\' ),
);
$args = array(
\'hierarchical\' => true,
\'labels\' => $labels,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'sitecat\' ),
);
register_taxonomy( \'site_category\', array( \'post\', \'page\', \'attachment\', \'revision\', \'nav_menu_item\' ), $args );
}
add_action( \'init\', \'PF_register_post_types\', 1 );
在我的页脚中。php我添加了以下代码:
$GLOBALS[\'wp_query\']->request
我在访问mydomain时得到了这个结果。com/测试:
SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_name = \'testing\' AND wp_posts.post_type = \'post\' ORDER BY wp_posts.post_date DESC
$pf\\U override变量显示$pf\\U override->要设置为
array( [0] => 1, [1] => 2, [2] => 3 )
最合适的回答,由SO网友:Playforward 整理而成
好吧,在深入研究代码之后,tax\\u查询似乎被阻止在单个帖子/页面上,这就是为什么我要关注这个问题。
If you see the ability to run tax_query in all circumstances as beneficial, please voice your opinion on trac.
请参见此处的票证:
http://core.trac.wordpress.org/ticket/24819我通过复制单数帖子的核心代码,并将其附加到join和where子句,成功地绕过了这个问题。新代码如下:
<?php
global $pf_override;
class PF_override {
public $site_terms = array();
public $sql_join = \'\';
public $sql_where = \'\';
function __construct() {
// add filters
add_filter( \'init\', array( &$this, \'populates_site_terms\' ) );
add_filter( \'pre_get_posts\', array( &$this, \'filter_query\' ) );
add_filter( \'posts_join\', array( &$this, \'filter_join\' ) );
add_filter( \'posts_where\', array( &$this, \'filter_where\' ) );
}
function populates_site_terms() {
// my actual function does some processing and
// checks caches and combines IDs from multiple
// different sources here and then sets site_terms
$this->site_terms = array( 1, 2, 3 );
}
function filter_query( $query ) {
// not the main query?
if ( !$query->is_main_query() ) {
return $query;
}
// have terms to filter by?
if ( !empty( $this->site_terms ) ) {
// construct tax_query
$tax_query = array(
\'taxonomy\' => \'site_category\',
\'field\' => \'id\',
\'terms\' => $this->site_terms,
\'operator\' => \'IN\'
);
// this needs to be an array of arrays
$taxquery = array(
$tax_query
);
// set this..
$query->set( \'tax_query\', $taxquery );
if ( $query->is_singular ) {
global $wpdb;
$q = &$query->query_vars;
$q[\'suppress_filters\'] = false;
$query->parse_tax_query( $q );
$clauses = $query->tax_query->get_sql( $wpdb->posts, \'ID\' );
$this->sql_join .= $clauses[\'join\'];
$this->sql_where .= $clauses[\'where\'];
}
}
return $query;
}
function filter_join( $join ) {
if ( !empty( $this->sql_join ) ) {
$join .= $this->sql_join;
}
return $join;
}
function filter_where( $where ) {
if ( !empty( $this->sql_where ) ) {
$where .= $this->sql_where;
}
return $where;
}
}
// not the admin? init class
if ( !is_admin() ) {
$pf_override = new PF_override();
}
?>