通过WP_Query

时间:2019-03-26 21:34:44

标签: 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] => 
    [embed] => 
    [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] => 
    [embed] => 
    [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的参数,一个是动态生成的,另一个是对其进行硬编码的。

0 个答案:

没有答案