Использование динамических множественных атрибутов с помощью WP_Query

#php #wordpress #woocommerce

#php #wordpress #woocommerce

Вопрос:

У меня есть форма, в которой я нажимаю на атрибут, и на основе этого щелчка я изменяю свой запрос для получения соответствующих результатов

Приведенный ниже массив генерируется динамически на основе выбора пользователя

 Array
(
    [tax_query] => Array
        (
            [0] => Array
                (
                    [taxonomy] => pa_timing
                    [field] => slug
                    [terms] => Array
                        (
                            [0] => day
                        )

                )

            [1] => Array
                (
                    [taxonomy] => pa_size
                    [fields] => slug
                    [terms] => Array
                        (
                            [0] => s
                        )

                )

        )

    [post_type] => product
    [post_status] => publish
    [product_cat] => pads
    [posts_per_page] => -1
)
  

На основе приведенного выше массива, сгенерированного WP_Query

 SELECT   vsrc_posts.* FROM vsrc_posts  LEFT JOIN vsrc_term_relationships ON (vsrc_posts.ID = vsrc_term_relationships.object_id)  LEFT JOIN vsrc_term_relationships AS tt1 ON (vsrc_posts.ID = tt1.object_id)  LEFT JOIN vsrc_term_relationships AS tt2 ON (vsrc_posts.ID = tt2.object_id) WHERE 1=1  AND ( 
  vsrc_term_relationships.term_taxonomy_id IN (351) 
  AND 
  tt1.term_taxonomy_id IN (259,263,274,314) 
  AND 
  tt2.term_taxonomy_id IN (348)
) AND vsrc_posts.post_type = 'product' AND ((vsrc_posts.post_status = 'publish')) GROUP BY vsrc_posts.ID ORDER BY vsrc_posts.post_date DESC

Array
(
    [tax_query] => Array
        (
            [0] => Array
                (
                    [taxonomy] => pa_timing
                    [field] => slug
                    [terms] => Array
                        (
                            [0] => day
                        )

                )

            [1] => Array
                (
                    [taxonomy] => pa_size
                    [fields] => slug
                    [terms] => Array
                        (
                            [0] => s
                        )

                )

        )

    [post_type] => product
    [post_status] => publish
    [posts_per_page] => -1
    [product_cat] => pads
    [error] => 
    [m] => 
    [p] => 0
    [post_parent] => 
    [subpost] => 
    [subpost_id] => 
    [attachment] => 
    [attachment_id] => 0
    [name] => 
    [static] => 
    [pagename] => 
    [page_id] => 0
    [second] => 
    [minute] => 
    [hour] => 
    [day] => 0
    [monthnum] => 0
    [year] => 0
    [w] => 0
    [category_name] => 
    [tag] => 
    [cat] => 
    [tag_id] => 
    [author] => 
    [author_name] => 
    [feed] => 
    [tb] => 
    [paged] => 0
    [meta_key] => 
    [meta_value] => 
    [preview] => 
    [s] => 
    [sentence] => 
    [title] => 
    [fields] => 
    [menu_order] => 
     => 
    [category__in] => Array
        (
        )

    [category__not_in] => Array
        (
        )

    [category__and] => Array
        (
        )

    [post__in] => Array
        (
        )

    [post__not_in] => Array
        (
        )

    [post_name__in] => Array
        (
        )

    [tag__in] => Array
        (
        )

    [tag__not_in] => Array
        (
        )

    [tag__and] => Array
        (
        )

    [tag_slug__in] => Array
        (
        )

    [tag_slug__and] => Array
        (
        )

    [post_parent__in] => Array
        (
        )

    [post_parent__not_in] => Array
        (
        )

    [author__in] => Array
        (
        )

    [author__not_in] => Array
        (
        )

    [ignore_sticky_posts] => 
    [suppress_filters] => 
    [cache_results] => 1
    [update_post_term_cache] => 1
    [lazy_load_term_meta] => 1
    [update_post_meta_cache] => 1
    [nopaging] => 1
    [comments_per_page] => 50
    [no_found_rows] => 
    [taxonomy] => pa_timing
    [term] => day
    [order] => DESC
)
  

Но когда я просто жестко кодирую массив и передаю его в WP_Query, он выдает

 SELECT   vsrc_posts.* FROM vsrc_posts  LEFT JOIN vsrc_term_relationships ON (vsrc_posts.ID = vsrc_term_relationships.object_id)  LEFT JOIN vsrc_term_relationships AS tt1 ON (vsrc_posts.ID = tt1.object_id)  LEFT JOIN vsrc_term_relationships AS tt2 ON (vsrc_posts.ID = tt2.object_id) WHERE 1=1  AND ( 
  vsrc_term_relationships.term_taxonomy_id IN (351) 
  AND 
  tt1.term_taxonomy_id IN (274) 
  AND 
  tt2.term_taxonomy_id IN (348)
) AND vsrc_posts.post_type = 'product' AND ((vsrc_posts.post_status = 'publish')) GROUP BY vsrc_posts.ID ORDER BY vsrc_posts.post_date DESC

Array
(
    [tax_query] => Array
        (
            [0] => Array
                (
                    [taxonomy] => pa_timing
                    [field] => slug
                    [terms] => Array
                        (
                            [0] => day
                        )

                )

            [1] => Array
                (
                    [taxonomy] => pa_size
                    [field] => slug
                    [terms] => Array
                        (
                            [0] => s
                        )

                )

        )

    [post_type] => product
    [post_status] => publish
    [posts_per_page] => -1
    [product_cat] => pads
    [error] => 
    [m] => 
    [p] => 0
    [post_parent] => 
    [subpost] => 
    [subpost_id] => 
    [attachment] => 
    [attachment_id] => 0
    [name] => 
    [static] => 
    [pagename] => 
    [page_id] => 0
    [second] => 
    [minute] => 
    [hour] => 
    [day] => 0
    [monthnum] => 0
    [year] => 0
    [w] => 0
    [category_name] => 
    [tag] => 
    [cat] => 
    [tag_id] => 
    [author] => 
    [author_name] => 
    [feed] => 
    [tb] => 
    [paged] => 0
    [meta_key] => 
    [meta_value] => 
    [preview] => 
    [s] => 
    [sentence] => 
    [title] => 
    [fields] => 
    [menu_order] => 
     => 
    [category__in] => Array
        (
        )

    [category__not_in] => Array
        (
        )

    [category__and] => Array
        (
        )

    [post__in] => Array
        (
        )

    [post__not_in] => Array
        (
        )

    [post_name__in] => Array
        (
        )

    [tag__in] => Array
        (
        )

    [tag__not_in] => Array
        (
        )

    [tag__and] => Array
        (
        )

    [tag_slug__in] => Array
        (
        )

    [tag_slug__and] => Array
        (
        )

    [post_parent__in] => Array
        (
        )

    [post_parent__not_in] => Array
        (
        )

    [author__in] => Array
        (
        )

    [author__not_in] => Array
        (
        )

    [ignore_sticky_posts] => 
    [suppress_filters] => 
    [cache_results] => 1
    [update_post_term_cache] => 1
    [lazy_load_term_meta] => 1
    [update_post_meta_cache] => 1
    [nopaging] => 1
    [comments_per_page] => 50
    [no_found_rows] => 
    [taxonomy] => pa_timing
    [term] => day
    [order] => DESC
)
  

Который, я ожидаю, выдаст WP_Query. проблема в том, что WP_Query не генерирует динамический запрос. Есть ли что-то, чего мне не хватает

 $dayArg = array(
            'tax_query'           => array(
                array(
                    'taxonomy'    => 'pa_timing',
                    'field'       => 'slug',
                    'terms'       => array( 'day' )
                ),
                array(
                    'taxonomy'    => 'pa_size',
                    'field'       => 'slug',
                    'terms'       => array( 's' )
                )
            ),
            'post_type'           => 'product',
            'post_status'         => 'publish',
            'posts_per_page'      => - 1,
            'product_cat'         => 'pads'
        )
  

Это аргумент, который я передаю в WP_Query, один генерирующий динамически, а другой жестко кодирующий его.

Комментарии:

1. @LoicTheAztec Я обновил свой вопрос соответствующими аргументами WP_Query

2. Я обновил свой ответ, я использую функцию.push () для генерации tax_query надеюсь, я прояснил свой вопрос