Home Forums General Issues array_intersect() with WP_Query displays every post


array_intersect() with WP_Query displays every post

    I’m developing a filter system for some products created through custom post types on a website that have custom taxonomies. They are divided into two sections, market and function you click on an object in the first section, it gets added in an array used for $_POST['taxonomy']. The other section adds to the same array, so I’m trying to use array_intersect() to divide the results in two arrays, since I’m using a tax_querywith 'relation' => 'AND' later on.

    The two tax_queries are based on those two sections, market and function.

    The results I’m getting is always every single product added (96 in total). I’m using AJAX for the function below. (The JS used is just processing the results and is working as suspected, so I’m sure there’s something wrong with the PHP down below).

    	function largo_filter() {
    		// The array of the selected taxonomies on the product search page
    		$taxonomy = json_decode(stripslashes($_POST['taxonomy']));
    		// These are the two sections, market and filter, divided into two arrays (the taxonomies actually have an ACF field called 'area' which I want to base them on, automatically, but haven't figured that out yet)
    		$market_array = array('butik', 'design', 'farg-lack', 'fordon', 'grafisk produktion', 'lakemedel', 'livsmedel', 'plast', 'textil');
    		$filter_array = array('doseringssystem', 'glansmatning', 'kulormatning', 'labbutrustning', 'programvaror', 'skak-mix', 'skikttjocklek', 'visuell-bedomning');
    		// Match which taxonomies from the search results are equal to the ones in the two sections
    		$market_match = array_intersect( $market_array, $taxonomy );
    		$filter_match = array_intersect( $filter_array, $taxonomy );
    		// Get all custom taxonomies called 'products_category' (that's ANOTHER custom taxonomy I'm using, to divide the search results into different parts (they each have a headline, shown in the search results on the website)
    		$terms = get_terms(
    				'taxonomy' => 'products_category',
    				'hide_empty' => true,
    		$num_posts = 0;
    		// Loop through every term
    		foreach( $terms as $term ) {
    			$args = array(
    				'posts_per_page'		=> -1,
    				'post_type'					=> 'produkt',
    				'products_category'	=> $term->slug,
    				'orderby'						=> 'title',
    				'order'							=> 'ASC',
    				// I want the products showing to have any of the terms from the market section AND any of the terms in the function section to be the results
    				'tax_query' => array(
    					'relation' => 'AND',
    						'taxonomy'	=> 'products_tag',
    						'field'			=> 'slug',
    						'terms'			=> $market_match,
    						'operator'	=> 'OR',
    						'taxonomy'	=> 'products_tag',
    						'field'			=> 'slug',
    						'terms'			=> $filter_match,
    						'operator'	=> 'OR',
    			$filter_query = new WP_Query($args);
    			$num_posts = $num_posts + $filter_query->found_posts;
    			if( $filter_query->have_posts() ):
    				while( $filter_query->have_posts() ) : $filter_query->the_post();
    					$titles[] = get_the_title();
    					$ids[] = get_the_ID();
    					$product_categories = get_the_terms( get_the_ID(), 'products_category' );
    					$product_category = array_pop( $product_categories );
    					$categories[] = $product_category->name;
    					$category_slugs[] = $product_category->slug;
    					$permalinks[] = get_permalink();
    					if( $image_field = get_field('images') ) {
    						$images[] = $image_field[0]['url'];
    					} else {
    						$images[] = get_template_directory_uri() . '/dist/img/ingen_bild.jpg';
    					$descriptions[] = get_field('short-description');
    		$response = array(
    			'success' => true,
    			'titles' => $titles,
    			'ids' => $ids,
    			'categories' => $categories,
    			'category_slugs' => $category_slugs,
    			'permalinks' => $permalinks,
    			'images' => $images,
    			'descriptions' => $descriptions,
    			'taxonomy' => $taxonomy,
    			'num_posts' => $num_posts
    		// generate the response
    		print json_encode($response);
    		// IMPORTANT: don't forget to "exit"
  • Your operator should be ‘IN’

      'taxonomy' => 'products_tag',
      'field' => 'slug',
      'terms' => $market_match,
      'operator' => 'IN',
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘array_intersect() with WP_Query displays every post’ is closed to new replies.