WordPress + ACF:带有WP_Query的array_intersect()

时间:2016-06-03 14:28:25

标签: php arrays ajax wordpress advanced-custom-fields

SCENARIO

我正在为通过自定义帖子类型on a website创建的某些产品开发过滤系统,这些产品具有自定义分类。它们分为两个部分,市场功能。如果单击第一部分中的对象,则会将其添加到用于$ _POST ['taxonomy']的数组中。另一部分添加到同一个数组中,因此我尝试使用array_intersect()将结果划分为两个数组,因为我之后使用tax_query'relation' => 'AND'。< / p>

这两个tax​​_queries基于这两个部分,市场和功能。

问题

我得到的结果总是添加每一件产品(共96件)。我正在使用AJAX来实现下面的功能。 (使用的JS只是处理结果并且正在怀疑,所以我确定下面的PHP有问题。)

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(
        array(
            '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',
                array(
                    'taxonomy'  => 'products_tag',
                    'field'         => 'slug',
                    'terms'         => $market_match,
                    'operator'  => 'OR',
                ),
                array(
                    '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');

            endwhile;

        endif;

        wp_reset_postdata();

    }

    $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"
    exit;

}

0 个答案:

没有答案