tax_query not working on main query

Well it seems after digging in to the code, tax_query is being blocked on single post/page pages which is why I’m running in to this issue.

If you see the ability to run tax_query in all circumstances as beneficial, please voice your opinion on trac.

See ticket here:
http://core.trac.wordpress.org/ticket/24819

I managed to get around this by duplicating the core code for singular posts and appending to the join and where clauses. The new code is as follows:

<?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();
}

?>

Leave a Comment