How to search by meta and custom taxonomy name – wordpress search?

I am building a ajax search that uses wp_query and I need to be able to filter using custom meta and custom taxonomy names. I’ve gotten the custom meta part working but cannot get it to filter using the taxonomy names. What I’ve got so far –

      $useCustomJoins = false;

       add_action( 'pre_get_posts', function( $q )
        {
            if( $title = $q->get( '_meta_or_title' ) )
            {
                add_filter( 'get_meta_sql', function( $sql ) use ( $title )
                {
                    global $wpdb;

                    // Only run once:
                    static $nr = 0; 
                    if( 0 != $nr++ ) return $sql;

                    // Modified WHERE
                    $sql['where'] = sprintf(
                        " AND ( %s OR %s ) ",
                        $wpdb->prepare( "{$wpdb->posts}.post_title like '%%%s%%'", $title),
                        mb_substr( $sql['where'], 5, mb_strlen( $sql['where'] ) )
                    );

                    return $sql;
                });
            }
        });

        add_filter('posts_join', array('CrownResources', 'filterPostsJoin'), 10, 2);
        add_filter('posts_where', array('CrownResources', 'filterPostsWhere'), 10, 2);
        add_filter('posts_groupby', array('CrownResources', 'filterPostsGroupby'), 10, 2);

        public static function filterPostsJoin($join, $query) {
            global $wpdb, $useCustomJoins;
            if($useCustomJoins || (is_main_query() && is_search())) {
                $join .= "
                    LEFT JOIN 
                    ( 
                        {$wpdb->term_relationships}
                        INNER JOIN 
                        {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id 
                        INNER JOIN 
                        {$wpdb->terms} ON {$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id 
                    ) 
                    ON {$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id ";
            }
            return $join;
        }

        public static function filterPostsWhere($where, $query) {
            global $wpdb, $useCustomJoins;
            if($useCustomJoins || (is_main_query() && is_search())) {
                $userWhere = self::getUserPostsWhere();
                $queryS = !empty(get_query_var('s')) ? get_query_var('s') : get_query_var('_meta_or_title');
                $where .= " OR (
                    {$wpdb->term_taxonomy}.taxonomy IN('resource_pregnancy_taxonomy', 'resource_counseling_taxonomy', 'resource_classes_taxonomy', 'resource_supplies_taxonomy', 'resource_clothing_taxonomy', 'resource_food_taxonomy', 'resource_housing_taxonomy', 'resource_medical_taxonomy', 'resource_substance_taxonomy', 'adoption_taxonomy', 'employment_taxonomy') 
                    AND
                    {$wpdb->terms}.name LIKE '%".esc_sql($queryS)."%'
                    {$userWhere}
                )";
            }
            return $where;
        }

        protected static function getUserPostsWhere() {
            global $wpdb;
            $userId = get_current_user_id();
            $sql = '';
            $status = array("'publish'");
            if(0 !== $userId) {
                $status[] = "'private'";
                $sql .= " AND {$wpdb->posts}.post_author = ".absint($userId);
            }
            $sql .= " AND {$wpdb->posts}.post_status IN( ".implode(',', $status)." ) ";
            return $sql;
        }

        public static function filterPostsGroupby($groupby, $query) {
            global $wpdb, $useCustomJoins;
            if($useCustomJoins || (is_main_query() && is_search())) {
                $groupby = "{$wpdb->posts}.ID";
            }
            return $groupby;
        }

usage:

    global $post, $useCustomJoins;
        $original_post = $post;

        // query for resource
        $queryArgs = array(
            'post_type' => 'resource',
            'posts_per_page' => -1,
            'orderby' => 'menu_order',
            'order' => 'ASC',
            'post_status' => 'publish'
        );

        //if taxonomies
        if (!empty($atts['taxonomies'])){
            $queryArgs['tax_query'] = array();

            foreach ($atts['taxonomies'] as $tax){
                $queryArgs['tax_query'][] = array(
                    'taxonomy' => $tax['name'],
                    'field' => 'term_id',
                    'terms' => $tax['terms'],
                    'operator' => 'AND',
                );
            }
        }

        // filter by meta as well as title
        $meta_query = array();

        $meta_query[] = array(
            'key' => 'resource_description',
            'value' => $atts['search'],
            'compare' => 'LIKE'
        );

        $meta_query[] = array(
            'key' => 'resource_address',
            'value' => $atts['search'],
            'compare' => 'LIKE'
        );

        $meta_query[] = array(
            'key' => 'resource_date_time',
            'value' => $atts['search'],
            'compare' => 'LIKE'
        );

        $meta_query[] = array(
            'key' => 'resource_phone',
            'value' => $atts['search'],
            'compare' => 'LIKE'
        );

        $meta_query[] = array(
            'key' => 'resource_email',
            'value' => $atts['search'],
            'compare' => 'LIKE'
        );

        $meta_query[] = array(
            'key' => 'resource_website',
            'value' => $atts['search'],
            'compare' => 'LIKE'
        );

        //if there is more than one meta query 'or' then
        if(count($meta_query) > 1) {
            $meta_query['relation'] = 'OR';
        }


        //if there's a search
        if (!empty($atts['search'])){
            // $queryArgs['s'] = $atts['search'];
            $queryArgs['_meta_or_title'] = $atts['search']; //not using 's' anymore
            $queryArgs['meta_query'] = $meta_query;
            $useCustomJoins = true;
        }

        $resourceQuery = new WP_Query($queryArgs);

        $useCustomJoins = false;

Any idea on how to get them working together? I can get the meta filter to search properly but once I add the taxonomy it still filters but not correctly.

Read more here: How to search by meta and custom taxonomy name – wordpress search?

Leave a Reply

Your email address will not be published. Required fields are marked *