• Resolved darrinb

    (@dbmartin)


    Is there any way to filter all post queries and add a specific taxonomy? Case in point, I have a taxonomy object of “Region” with terms like “Global”, “Americas”, “Europe”, etc. (Users designate their region in their user profile through custom fields which is stored as a usermeta value and added the user wp_cache object.)

    The idea being that as the signed-in user navigates the site, they’d only see content designated within their region. Obviously I could write a custom query per page to include their region, but I was wondering if there was a way to tap into the query object and add the taxonomy requirement regardless of what the query was for (e.g, post, custom post type, archive page, etc.). Something along the lines of:

    if(!is_admin()) {
        $args = array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'region',
                    'field' => 'slug',
                    'terms' => array( 'region2' ),
                ),
                array(
                    'taxonomy' => 'post_format',
                    'field' => 'slug',
                    'terms' => array( 'post-format-quote' )
                )
        );
        $query->set('tax_query',$args);
        return $query;
    }

    But this doesn’t seem to work.

    Any help would be much appreciated! Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • I believe that you could use a filter on ‘posts_join’ to add the necessary SQL to every query. The filter could be this:

    function mam_posts_join ($join) {
       global $mam_global_join;
       if ($mam_global_join) $join .= " $mam_global_join";
       return $join;
    }
    add_filter('posts_join','mam_posts_join');

    Then, you would assign $mam_global_join the necessary SQL and it would be added to every query. You would need code to only apply the filter to certain queries. For example, if you tried to add the filter to a query that did not include the wp_posts table, it would force a null result.

    Thread Starter darrinb

    (@dbmartin)

    This is the code I ended up using. Works great.

    function content_filter( $query ) {
    
        // if we're not in the Dashboard
        if( !is_admin() ) {
            global $user_ID;  // get the current user ID
    
            // we only need to modify the query for logged in users
            if ( !is_user_logged_in() || !$user_ID || $user_ID < 1) {
                return $query;
            }
    
            // get the user from $wp_object_cache
            $user = wp_cache_get($user_ID, 'users');
            if(!$user){
                return $query;
            }
    
            // get Region set in user key in $wp_object_cache
            $user_region = $user->user_region;
    
            $query->set('tax_query', array(
                'relation' => 'OR',
                array(
                      'taxonomy' => 'region',
                      'field' => 'slug',
                      'terms' => $user_region,
                      ),
                array(
                      'taxonomy' => 'region',
                      'field' => 'slug',
                      'terms' => 'all-regions',
                      )
                )
            );
            return $query;
        }
    }
    add_filter( 'pre_get_posts', 'content_filter' );

    I’m really glad you raised this as there was so little documentation explaining this. You have saved me a lot of time. Thanks

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Filter Queries by Custom Taxonomy’ is closed to new replies.